React Redux意外令牌,预期为“,”

时间:2018-10-01 23:01:12

标签: reactjs redux react-redux redux-actions

我正在使用带有react-redux和redux-actions的React。

我有以下减速器不断告诉我

  

意外的令牌,预期为“,”

但我不确定为什么。

>=

comments.js (reducer):

引起麻烦的动作是import { handleActions } from "redux-actions"; import { GET_COMMENTS, SET_COMMENTS, ADD_COMMENT } from "../constants/actionTypes"; export default handleActions( { [GET_COMMENTS]: (state, action) => state, [ADD_COMMENT]: (state, action) => { const comments = { ...state.comments, action.payload }; return { ...state, comments }; }, [SET_COMMENTS]: (state, action) => Boolean(action.payload) ? action.payload : state }, {} ); 。我已经尝试了以下方法:

ADD_COMMENT

[ADD_COMMENT]: (state, action) => {
  ...state,
  comments: {
    ...state,
    action.payload
  }
}

以及:

[ADD_COMMENT]: (state, action) => ({
      ...state,
      comments: {
        ...state,
        action.payload
      }
})

我看不到为什么我的代码不正确,我的原子弹说的是动作和有效载荷之间的点,但是我不确定。

动作创建者仅以以下格式返回ADD_COMMENT类型和单个注释的有效载荷:

[ADD_COMMENT]: (state, action) => {
  return {
      ...state,
      comments: {
        ...state,
        action.payload
      }
  }
}

1 个答案:

答案 0 :(得分:5)

您正试图在没有键的对象中包含变量:

// This is fine
const comments = {
  ...state.comments
}

// This is not
const comments = {
  actions.payload
}

// Possible alternative:
const comments = {
  ...state.comments,
  payload: actions.payload
}

// Or if you want to destructure `actions.payload`:
const comments = {
  ...state.comments,
  ...actions.payload
}
相关问题