React Redux - 将多个存储增强器传递给 createStore() 时出错

2023-11-27

我有一个运行 redux 和 thunk 的 React 应用程序,一切都运行良好。我需要在页面重新加载时保留存储状态,以便数据不会丢失,因此创建了一个函数,该函数将数据存储在本地存储中,然后返回准备添加到 createStore 的数据(https://stackoverflow.com/a/45857898/801861)。数据存储工作正常并返回准备设置日期的对象。在 createStore 添加数据对象时,反应无法编译并出现以下错误:

错误:看起来您正在将多个存储增强器传递给 createStore()。不支持此操作。相反,将它们组合成一个函数

这是当前代码返回错误:

const store = createStore(reducers, LoadState, applyMiddleware(thunk) );

//Error: It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function

我原来运行的代码:

const store = createStore(reducers, applyMiddleware(thunk) );

我尝试修复此问题,遵循我在网上发现的一些类似问题,编译但破坏了最初工作正常的站点代码:

const composeEnhancers = LoadState || compose;
const store = createStore(reducers, composeEnhancers( applyMiddleware(thunk) ) );
//Error: Actions must be plain objects. Use custom middleware for async actions.

不确定我需要更改什么才能使其正常工作,非常感谢任何帮助。


我认为您正在遵循一个在以前版本的 redux 中执行任务的教程。

您需要创建一个 composeEnhancers ,它将接受您的 Redux Devtools 扩展,如图所示

const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

从“redux”导入 compose 后,显然如图所示 -

import {createStore, compose, applyMiddleware} from 'redux';

然后,您可以创建商店,如下所示 -

const Store = createStore(rootReducer, composeEnhancer(applyMiddleware(thunk)))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React Redux - 将多个存储增强器传递给 createStore() 时出错 的相关文章

随机推荐