使用redux appilcation我有以下数据结构,使用fromJS()
将其转换为不可变对象import { Map, push, fromJS } from 'immutable';
const initialState = fromJS({
filter: "cards",
contacts: [
{
name: "Someone 1",
email: "someone@example.com",
phone: "+1 900 78601",
location: "Los Angeles"
},
]});
我的减速机看起来像这样:
function contactsReducer(state = initialState, action) {
switch (action.type) {
case ADD_CONTACT:
return state.get('contacts').push(Map(
{
name: "Someone 1",
email: "someone@example.com",
phone: "+1 900 78601",
location: "Los Angeles"
}
));
default:
return state;
}}
我无法弄清楚如何定位联系人数组并为其添加更多联系人。我写的reducer修改了新状态的整个结构。有什么帮助吗?
答案 0 :(得分:0)
您可以使用地图上的set
在您调用contacts
后使用新更新的列表更新push
字段。
state.set('contacts').push(Map(
{
name: "Someone 1",
email: "someone@example.com",
phone: "+1 900 78601",
location: "Los Angeles"
}
))
答案 1 :(得分:0)
我根据Alex Parij给出的答案找到了答案,需要稍作修改。这是:
state.set('contacts', state.get('contacts').push(Map(
{
name: "Someone 1",
email: "someone@example.com",
phone: "+1 900 78601",
location: "Los Angeles"
}
)))