reduxで配列操作する時のメモ
目的
reduxで配列操作する時にサラッと確認したいメモです。
Reducerのみ
cardReducer.js
import {
ADD_CARD,
UPDATE_CARD,
DELETE_CARD,
} from './cardTypes'
const initialState = {
imageList: []
}
const cardReducer = (state = initialState, action) => {
switch (action.type) {
// 要素の追加
case ADD_CARD:
return {
...state,
imageList: [...state.imageList, action.payload]
}
// 要素の更新
// 要素のIDで更新対象を確定
case UPDATE_CARD:
return {
...state,
imageList: state.imageList.map(el => el.id === action.payload.id ? action.payload : el)
}
// 要素の削除
// 要素のIDで削除対象を確定
case DELETE_CARD:
return {
...state,
imageList: state.imageList.filter((item) => item.id !== action.payload)
}
default: return state
}
}
export default cardReducer
Author And Source
この問題について(reduxで配列操作する時のメモ), 我々は、より多くの情報をここで見つけました https://qiita.com/penpenta/items/fe844cdcbfed15a5bf8c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .