React Redux 意外令牌,预期“,”
React Redux Unexpected token, expected ","
我将 React 与 react-redux 和 redux-actions 结合使用。
我有以下减速器一直告诉我
unexpected token, expected ","
但我不确定为什么。
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]: (state, action) => {
return {
...state,
comments: {
...state,
action.payload
}
}
}
我不明白为什么我的代码不正确,我的原子 linter 说它是动作和有效负载之间的点,但我不确定。
动作创建者只是 returns 一种 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"
}
您正在尝试在没有键的情况下在对象中包含变量:
// 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
}
我将 React 与 react-redux 和 redux-actions 结合使用。
我有以下减速器一直告诉我
unexpected token, expected ","
但我不确定为什么。
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]: (state, action) => {
return {
...state,
comments: {
...state,
action.payload
}
}
}
我不明白为什么我的代码不正确,我的原子 linter 说它是动作和有效负载之间的点,但我不确定。
动作创建者只是 returns 一种 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"
}
您正在尝试在没有键的情况下在对象中包含变量:
// 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
}