如何在redux reducer中推送字符串数组?

时间:2017-03-23 10:21:37

标签: reactjs redux react-redux redux-form

我使用了一个将ipfs哈希存储为数组的reducer。我无法推送值数组,而不是新哈希覆盖现有哈希值。 Reducer代码是

const INITIAL_STATE = {
    fetching : false,
    fetched : false,
    ipfsHash : [],
    error : null
}
export default (state=INITIAL_STATE,action) =>{
        switch(action.type){
            case 'FILE_UPLOAD_START':
                return {
                    ...state,
                    fetching:true
                }
            case 'FILE_UPLOAD_SUCCESS':
                return {
                    ...state,
                    fetching : false,
                    fetched : true,
                    ipfsHash : action.payload // IPFS is replaced rather than pushing into the array
                }
            case 'FILE_UPLOAD_ERROR':
                return {
                    ...state,
                    fetching : false,
                    fetched : false,
                    error : action.payload
                }
            default:
                return {...state}
        }
    }

将哈希推入数组的任何想法????

1 个答案:

答案 0 :(得分:3)

您可以使用点差运算符

return {
  ...state,
  fetching : false,
  fetched : true,
  ipfsHash : [...state.ipfsHash, ...action.payload]
}

concat

return {
  ...state,
  fetching : false,
  fetched : true,
  ipfsHash : state.ipfsHash.concat(action.payload)
}