繁体   English   中英

React Redux array.push 的问题

[英]Issue with React Redux array.push

我遇到了一个问题,当我尝试使用 .push(newvalue) 更新存储中的数组时。 当我推送新值时,状态从数组更新为数组长度的值。

更新前的状态:['asd', 'sdf', 'wer']

之后的状态: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 方法修改了它被调用的数组并返回新的长度。 你想要的是像 concat() 这样的东西。

推送: https : //www.w3schools.com/jsref/jsref_push.asp

连接: https : //www.w3schools.com/jsref/jsref_concat_array.asp

compliments: state.compliments.concat(action.payload)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM