我知道如何與推送方法做:如何使用ES6 ...方法將對象添加到數組中?
import * as types from '../constants/ActionTypes'
const initialState = {
designBox: [],
}
import * as types from '../constants/ActionTypes'
const initialState = {
designBox: [],
}
export default function(state = initialState, action) {
switch (action.type) {
case types.CREATE_DESIGN_BOX:
let newState = Object.assign({}, state);
newState.designBox.push(action.payload)
return newState
default:
return state
}
}
但我不知道如何與...
方法
現在我的代碼有問題呢,designBox不能添加對象,
它只有一個項目,因爲它只是由新的action.payload
import * as types from '../constants/ActionTypes'
const initialState = {
designBox: [],
}
export default function(state = initialState, action) {
switch (action.type) {
// action.payload format -> { width:200,height:300,text:'abc'}
case types.CREATE_BOX:
return {
...state,
designBox: [action.payload]
}
default:
return state
}
}
覆蓋我怎樣才能做到這一點用......的方法?
它是一種常見的方式初始化狀態是數組不反對? – user2492364
如果它只有一個鍵有一個數組作爲值,那麼沒有理由將您想要訪問的內容嵌入到比所需更深的層次。 – Scarysize