我遇到了一個問題,當我嘗試使用.push(newvalue)更新存儲中的數組時。當我推新值時,狀態從數組更新爲數組長度的值。更新之前問題React Redux array.push
州:[ 'ASD', 'SDF', '疫情週報']
狀態後:動作4
:
export function updateLocalCompliments(newCompliment) {
return ({
type: LOCAL_COMPLIMENT,
payload: newCompliment
})
}
減速器:(state.compliments是實際數組)
const INITIAL_STATE = {
compliments: []
}
function compliments(state=INITIAL_STATE, action) {
switch (action.type) {
case LOCAL_COMPLIMENT:
return (
Object.assign({}, state, {
compliments: state.compliments.push(action.payload)
})
)
default:
return state;
}
}
'push'不返回所述突變的陣列,但其更新長度。您需要以不同的方式處理:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/push – m90
'push'不應該與react-redux一起使用,因爲它會改變數組 – MinusFour