admin管理员组文章数量:1302269
I'm learning Redux and having trouble trying to figure out how to add a key pair to a dictionary:
//Page Dictionary
{
randomId1: { aPageObj },
randomId2: { aPageObj }, etc
}
I tried this but that doesn't work
//action
export function addPage( pageId, pageObj ) {
return { type: types.ADD_PAGE, pageId, pageObj };
}
//reducer
case types.ADD_PAGE: {
return Object.assign({}, state, {
pages: {
...state.pages, action.pageId: action.pageObj
}
});
}
What am I doing wrong? Still trying to figure out how to not mutate the state..
I'm learning Redux and having trouble trying to figure out how to add a key pair to a dictionary:
//Page Dictionary
{
randomId1: { aPageObj },
randomId2: { aPageObj }, etc
}
I tried this but that doesn't work
//action
export function addPage( pageId, pageObj ) {
return { type: types.ADD_PAGE, pageId, pageObj };
}
//reducer
case types.ADD_PAGE: {
return Object.assign({}, state, {
pages: {
...state.pages, action.pageId: action.pageObj
}
});
}
What am I doing wrong? Still trying to figure out how to not mutate the state..
Share Improve this question asked Aug 11, 2017 at 20:41 user1189352user1189352 3,88513 gold badges59 silver badges99 bronze badges2 Answers
Reset to default 10You need to use puted property name of object literal (with []
):
pages: {
...state.pages, [action.pageId]: action.pageObj
}
Furthermore if you can use object spreas syntax, you can omit Object.assign
, for example:
return {
...state,
pages: {
...state.pages, [action.pageId]: action.pageObj
}
}
Although we can store a map in redux, its not remended - https://github./reduxjs/redux/issues/1499.
本文标签: javascriptAdding a KeyValue to a Dictionary in ReduxStack Overflow
版权声明:本文标题:javascript - Adding a KeyValue to a Dictionary in Redux - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741649459a2390386.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论