javascript - Redux 重置操作 - 状态相等
问题描述
对于 React Redux,我有以下初始状态:
const inistialStateRedux = {
configuredFilters: {
data: {
countries: [],
divisions: [],
companies: [],
locations: [],
fields: [],
search: '',
},
},
};
现在我想创建一个 RESET 减速器。
它看起来像这样:
export const createReducer = (initialState, handlers) => (
state = initialState,
action
) => {
if (action.type in handlers) {
return handlers[action.type](state, action);
}
return state;
};
export const multiUse = (reducer, name = '') => (state = null, action) => {
if (action.name !== name) return state;
return reducer(state, action);
};
import {
createReducer
} from '../helper';
import * as Action from './actions';
import inistialStateRedux from '../inistialStateRedux';
export default createReducer({
data: {},
}, {
[Action.RESET_CONFIGURED_FILTERS]: (state) => ({
...state,
data: {
...inistialStateRedux.configuredFilters.data,
},
}),
});
但 Redux Devtools 显示,状态是平等的。我究竟做错了什么 ?
解决方案
在 Actions 中,您可以使用调度程序来重置表单。像这样:
import axios from 'axios';
import { ADD} from '../modelType';
import { reset } from 'redux-form';
export const add= formValues => async dispatch => {
const res = await axios.post('/api/model/', { ...formValues });
dispatch({
type: ADD,
payload: res.data
});
dispatch(reset('yourUniqueFormName'));
};
推荐阅读
- spring - 尝试使用 spring 服务器和云访问 .properties 文件。适用于本地仓库但不适用于 git 远程仓库
- heroku - Discord.js 机器人 | Heroku 部署错误 | npm 错误!缺少脚本:开始
- scala - 理解 Scala 隐式类
- git - git:在发布/标签中获取*新*贡献者(作者)列表
- python - 如何正确地在本地“部署”基于 Python 的服务器应用程序进行开发?
- jquery - 我的标题图像隐藏在固定顶部导航栏后面
- android - 如何正确实现 View.OnClickListener 并将其与 TextView 一起使用?
- angular - openLayers addFeature 从 xmlhttprequest 角度的向量源
- mouseevent - 在 A-Frame 中单击即可获取场景的坐标
- python - 如何为 numpy.where() 提供多个条件