javascript - useEffect 重写整个商店
问题描述
我有一个带有 3 个减速器的 redux 商店:
let reducers = combineReducers({
config: configReducer,
data: dataReducer,
currentState: gameStateRecuder})
let store = createStore(reducers, applyMiddleware(thunkMiddleware));
在这些减速器中的每一个中,初始存储都是空的,但是一旦安装了 App 组件,我就会用我收到的使用 usinguseEffect
来替换减速器内的每个初始存储。在每个 reducer 中都是这样的:axios.get
redux-thunk
let initialState = [];
const SET_STATE = 'SET_STATE';
const configReducer = (state = initialState, action) => {
switch (action.type) {
case SET_STATE: {
return { ...action.state};
}
default:
return state;
}
const setState = (state) => ({ type: SET_STATE, state });
export const getConfigState = () => (dispatch) => {
getAPI.getConfig() //I import getAPI with all the REST API logic//
.then(response => {
dispatch(setState(response));
})
};
App 触发器是:
const App = (props) => {
useEffect(() => {
props.getConfigState();
props.getDataState();
props.getGameState();
}, []);
return (
//JSX//
);
}
export default compose(connect(null, { getConfigState, getDataState, getGameState }))(App);
最后,我将每个 reducer 的状态替换为 promise 解决了最后一个的状态。我可以尝试使用 HOC 将应用程序再包装 2 次,该 HOC 除了重写精确减速器的状态外,什么都不做,但我仍然想了解是什么导致承诺影响除了他需要影响的其他减速器之外的其他减速器。
解决方案
一个愚蠢的错误,但也许有人有完全相同的问题 - 解决方案是为每个减速器提供不同的案例名称 -SET_STATE
需要分别成为SET_GAME_STATE
, 。我相信这是因为我对工作原理的误解。SET_CONFIG_STATE
SET_DATA_STATE
dispatch
推荐阅读
- sql-server - Mac OS X 上的 Asp.Net Core 连接到 SQL Server Linux Docker
- python - np.arange 的奇怪行为
- rest - 休息客户端步骤以获取 oauth 令牌并使用它从 pentaho 中的 API 获取数据
- python - 在 seaborn 热图中使用自定义步骤设置 yticklabels
- python-2.7 - SSLv3 与 Python 中的 ssl 库的连接
- html - 将忽略整个网站格式的文本的 css 参数
- symfony - 如何在路径中设置变量 - API 平台
- php - php file_get_contents 未捕获某些 cookie 组
- django - ModelForm required=True 未按预期运行
- c# - 如何在后面的代码中应用 materialDesign:ColorZoneAssist.Mode="Accent"