[英]Spread operator error with React and Redux
I'm fairly new to react and redux. 我对反应和还原还很陌生。 I'm working on a reducer and running into compilation errors when trying to use the spread operator.
我正在尝试使用精简器,并在尝试使用传播运算符时遇到编译错误。
export default function chaptersReducer(
state = { chapters: {}, isFetching: false, error: false },
action = {},
) {
switch (action.type) {
case REQUEST:
return { ...state, isFetching: true, error: false };
case SUCCESS:
return {
...state,
chapters: {
...state.chapters,
action.payload
},
isFetching: false
};
case FAILURE:
return { ...state, isFetching: false, error: true };
default:
return state;
}
}
The specific error I'm getting is this. 我得到的具体错误是这样的。 (inside the SUCCESS case)
(在“成功”案例中)
Syntax error: Unexpected token, expected , (27:44)
> 27 | chapters: {...state.chapters, action.payload},
| ^
Maybe I'm missing something extremely obvious, but can someone help me figure out what's going on? 也许我错过了一些非常明显的东西,但是有人可以帮我弄清楚发生了什么吗?
EDIT: I'm using create-react-app, redux, and react-redux 编辑:我正在使用create-react-app,redux和react-redux
chapters: {
...state.chapters,
action.payload
},
not really sure what you're going for, here 不太确定要干什么,在这里
chapters: {
...state.chapters,
...action.payload
},
maybe? 也许?
not specifying a key ( { someProp }
) is shorthand for { someProp: someProp }
and requires simple keys (ie action.payload
doesn't work) 不指定键(
{ someProp }
)是{ someProp: someProp }
简写,并且需要简单的键(即action.payload
不起作用)
either way, you need either a key there ie 无论哪种方式,您都需要一个钥匙,即
chapters: {
...state.chapters,
chapterN: action.payload
},
or to replace entirely 或完全取代
chapters: action.payload
edit: I see your comment 编辑:我看到你的评论
given action.payload
=> { someId: { ... some payload ... } }
, 给定
action.payload
=> { someId: { ... some payload ... } }
,
chapters: {
...state.chapters,
...action.payload
},
is probably pretty close to what you want. 可能与您想要的非常接近。 if you're also passing the
mangaId
as a field on the action ie { id: mangaId, payload: { ... some payload ... } }
, then you'll want 如果您还要在操作
mangaId
作为字段传递, mangaId
{ id: mangaId, payload: { ... some payload ... } }
,那么您将需要
chapters: {
...state.chapters,
[action.mangaId]: action.payload
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.