Gut*_*typ 10 javascript reducers reactjs
我有一个带有过滤器的 ReactJS 应用程序,并使用 RESET 函数来重置这些过滤器。
我也使用过:Redux、Redux Persist 和 React-router-dom。
如果我查看 Redux Devtools,它似乎有效。但是应用程序没有正确重新渲染,需要刷新 (f5)。
我想要实现的目标:用initialState 对象覆盖configuredFilters 对象。
这是我的根减速器:
const rootReducer = (state = initialState, action) => {
let newState = state;
if (action.type === 'RESET_FILTERS') {
storage.removeItem('persist:configuredFilters');
// eslint-disable-next-line no-param-reassign
newState = { ...newState,
configuredFilters: JSON.parse(JSON.stringify(initialState.configuredFilters))
};
}
return appReducer(newState, action);
};
Run Code Online (Sandbox Code Playgroud)
这是差异(我之前配置了两个国家):
这是对象(如果页面已加载,则为初始状态):
组件是用这个组件创建的:
/* eslint-disable max-len */
import React from 'react';
import { useSelector, shallowEqual, useDispatch } from 'react-redux';
import { Form, Select } from 'antd';
import PropTypes from 'prop-types';
import RenderTag from './RenderTag';
import * as Action from '../../store/configuredFilters/actions';
const propTypes = {
data: PropTypes.shape({
name: PropTypes.string.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
options: PropTypes.arrayOf(PropTypes.shape({})),
}).isRequired,
};
const { Option } = Select;
const useData = () => {
const dataFromRdx = useSelector(
(state) => ({
configuredFilters: state.configuredFilters,
}),
shallowEqual
);
return { dataFromRdx };
};
const FixedListSelect = ({
data: {
name, label, placeholder, options,
},
}) => {
const { dataFromRdx } = useData();
const {
configuredFilters: {
data: {
search: searchTerm,
},
},
} = dataFromRdx;
const dispatch = useDispatch();
const dispatchFns = {
setConfiguredFilters: (key, value) => {
dispatch(Action.setConfiguredFilters(key, value));
},
};
const setRdxVal = (id, currVal) => {
dispatchFns.setConfiguredFilters(id, currVal);
};
const isFullTextSearchMode = (searchTerm && searchTerm.length);
return (
<Form.Item
name={name}
label={label}
fieldKey={name}
>
<Select
allowClear
disabled={isFullTextSearchMode}
showSearch
tagRender={RenderTag}
mode="multiple"
placeholder={placeholder}
optionFilterProp="children"
filterOption={(input, option) => option.children.toLowerCase().includes(input.toLowerCase())}
onChange={(currVal) => { setRdxVal(name, currVal); }}
>
{(options || []).map((el) => <Option data-filterid={el.val} key={el.val} value={el.val}>{el.label}</Option>)}
</Select>
</Form.Item>
);
};
FixedListSelect.propTypes = propTypes;
export default FixedListSelect;
Run Code Online (Sandbox Code Playgroud)
这个组件的调用:
<FixedListSelect data={{
name: 'companies',
label: t('companies'),
placeholder: t('companies-placeholder'),
options: companies,
}}
/>
Run Code Online (Sandbox Code Playgroud)
有人可以提供帮助或至少给出提示吗?
归档时间: |
|
查看次数: |
464 次 |
最近记录: |