gpt4 book ai didi

reactjs - React Redux 意外 token ,预期 ","

转载 作者:行者123 更新时间:2023-12-02 08:04:59 26 4
gpt4 key购买 nike

我将 React 与 react-redux 和 redux-actions 结合使用。

我有以下 reducer 不断告诉我

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 说它是操作和有效负载之间的点,但我不确定。

action creator 仅返回 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
}

关于reactjs - React Redux 意外 token ,预期 ",",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52599994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com