如何将状态值存储为 redux 中的键值对..?
How to store state values as key value pair in redux..?
可能重复:
- redux - how to store and update a key/value pair
以上可能的重复项不能满足我的需要。
下面是我用来存储为对象数组的 redux reducer 代码:
case 'ADD_ITEM':
return {...state, elements:[...state.elements, action.appElements]}
其中 action.appElements
包含:
{id: '9aq05d', width: '100',height: '225'}
存储的对象数组如下所示:
elements: {
0: {
id: 9aq05d,
width: '100',
height: '225',
}
1: {
id: 8lk65f,
width: '200',
height: '787',
}
}
但我需要将值存储为 键值对,如下所示:
我需要 id 作为键。
elements: {
9aq05d: {
id: 9aq05d,
width: '100',
height: '225',
}
8lk65f: {
id: 8lk65f,
width: '200',
height: '787',
}
}
如何在redux store中存储这种键值对..?
提前致谢..
使用对象展开而不是数组展开。
case 'ADD_ITEM':
return {
...state,
elements: {
...state.elements,
[action.appElements.id]: action.appElements
}
}
但请记住,不能保证对象中键的顺序。
可能重复:
- redux - how to store and update a key/value pair
以上可能的重复项不能满足我的需要。
下面是我用来存储为对象数组的 redux reducer 代码:
case 'ADD_ITEM':
return {...state, elements:[...state.elements, action.appElements]}
其中 action.appElements
包含:
{id: '9aq05d', width: '100',height: '225'}
存储的对象数组如下所示:
elements: {
0: {
id: 9aq05d,
width: '100',
height: '225',
}
1: {
id: 8lk65f,
width: '200',
height: '787',
}
}
但我需要将值存储为 键值对,如下所示:
我需要 id 作为键。
elements: {
9aq05d: {
id: 9aq05d,
width: '100',
height: '225',
}
8lk65f: {
id: 8lk65f,
width: '200',
height: '787',
}
}
如何在redux store中存储这种键值对..?
提前致谢..
使用对象展开而不是数组展开。
case 'ADD_ITEM':
return {
...state,
elements: {
...state.elements,
[action.appElements.id]: action.appElements
}
}
但请记住,不能保证对象中键的顺序。