【发布时间】:2017-01-06 22:51:44
【问题描述】:
我有一个 reducer,它接收一个带有我需要更新状态的有效负载的操作。问题是我需要在状态中更新的数据是嵌套数据。 我在下面添加了我的减速器,并附上了一些评论以及到目前为止我尝试做的事情。
export default function(state=data, action){
switch (action.type) {
case 'UPDATE_CONTACT_INFO':
let appointment = state[action.appointmentIndex]; // This is the appointment that needs to be updated
appointment.notification.contactInfo = action.payload; // this is the data that needs to be updated with the payload. I tried updating it like this but then not sure how to add it to the state.
return state; // Somehow need to update the state with the new state
break;
default:
return state;
}
}
下面是我的初始数据结构,我将其作为默认状态传递给 reducer。
data = [
{
date: 'Friday, January 6',
time: '4:00 PM-5:00 PM',
notification:
{
contactInfo: [
{
displayMethod:"Phone Call",
method:"Phone",
value:"3473686552"
},
{
displayMethod:"Email",
method:"Email",
value:"memedoe@gmail.com"
}
]
}
},
{
date: 'Saturday, January 7',
time: '2:00 PM-6:00 PM',
notification:
{
contactInfo: [
{
displayMethod:"Phone Call",
method:"Phone",
value:"2123686552"
},
{
displayMethod:"Email",
method:"Email",
value:"johndoe@gmail.com"
}
]
}
}
];
reducer 数据中的 action.payload 与其中一个约会中的 contactInfo 数组的结构相同。 [Object, Object]
【问题讨论】:
标签: reactjs react-redux