我有一个问题,当我尝试使用.push(newvalue)更新商店中的数组时。当我按下新值时,状态从数组更新为数组长度的值。
更新前的状态:[' asd',' sdf',' wer']
状态:4
行动:
export function updateLocalCompliments(newCompliment) {
return ({
type: LOCAL_COMPLIMENT,
payload: newCompliment
})
}
reducer :( 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;
}
}
答案 0 :(得分:3)
这是因为push方法修改了调用它的数组并返回新的长度。你想要的就像concat()。
推送:https://www.w3schools.com/jsref/jsref_push.asp
concat:https://www.w3schools.com/jsref/jsref_concat_array.asp
compliments: state.compliments.concat(action.payload)