Reactjs 添加/删除表单后,初始表单值将被丢弃
我有一个Reactjs 添加/删除表单后,初始表单值将被丢弃,reactjs,redux,redux-form,Reactjs,Redux,Redux Form,我有一个NotificationList组件,它从存储在列表中的获取数据中呈现NotificationCard //NotificatitonList.js return this.props.notifications.list.map(n => { return ( <div> <NotificationCard key={n.id} form={`updateNotification_${n.id}`}
NotificationList
组件,它从存储在列表中的获取数据中呈现NotificationCard
//NotificatitonList.js
return this.props.notifications.list.map(n => {
return (
<div>
<NotificationCard
key={n.id}
form={`updateNotification_${n.id}`}
initialValues={n}
notfId={n.id}
/>
</div>
);
});
它起作用了。但是,如果我尝试在redux中存储的notifications.list
的顶部添加新的编号,它会被添加,但表单中的所有初始值也会被丢弃。如果我将新通知添加到列表的末尾,则效果良好。删除通知也是如此,只有删除列表中的最后一个通知,它才能正常工作。我不明白为什么会发生这种情况,我似乎没有在任何地方使用列表索引
减速器的相关部分:
const INITIAL_STATE = {list: [], ...};
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
...
case CREATE_NOTIFICATION:
return { ...state, list: [...state.list, action.payload] };
case DELETE_NOTIFICATION:
return {
...state,
list: state.list.filter(el => el.id !== action.id)
};
多谢各位。有什么想法吗
为了让您更好地了解我所拥有的:
通过使用
初始化
动作创建者,该动作创建者通过redux表单
作为道具传递,从而使其正常工作。只需将其与初始值一起放入组件didmount
:
componentDidMount() {
this.props.initialize(this.props.initialValues);
}
componentDidMount() {
this.props.initialize(this.props.initialValues);
}