React Redux意外的令牌,预期","

CHB*_*ser 2 reactjs redux react-redux redux-actions

我正在使用React with react-redux和redux-actions.

我有以下减速机一直告诉我

意外的令牌,预期","

但我不确定为什么.

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
  },
  {}
);
Run Code Online (Sandbox Code Playgroud)

导致麻烦的动作是ADD_COMMENT.我尝试过以下几种方法:

[ADD_COMMENT]: (state, action) => {
  ...state,
  comments: {
    ...state,
    action.payload
  }
}
Run Code Online (Sandbox Code Playgroud)

要么

[ADD_COMMENT]: (state, action) => ({
      ...state,
      comments: {
        ...state,
        action.payload
      }
})
Run Code Online (Sandbox Code Playgroud)

以及:

[ADD_COMMENT]: (state, action) => {
  return {
      ...state,
      comments: {
        ...state,
        action.payload
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么我的代码不正确,我在原子中的linter说它是动作和有效载荷之间的点,但我不确定.

动作创建者只是按以下格式返回一种ADD_COMMENT类型和各个注释的有效负载:

{
    "id": 3,
    "parent": 1,
    "author": "admin",
    "author_is_staff": true,
    "content": "This is a test comment using the API rather than the Admin page, with author specified, with view changed"
}
Run Code Online (Sandbox Code Playgroud)

cor*_*ard 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
}
Run Code Online (Sandbox Code Playgroud)