[英]Connecting Redux devtools and Thunk middleware to store
I am trying to connect redux-devtools to my store but I keep getting the following error: " It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function error."我正在尝试将 redux-devtools 连接到我的商店,但我不断收到以下错误:“看起来您正在将多个商店增强器传递给 createStore()。这是不受支持的。相反,请将它们组合成一个函数错误。 ”
*Using Thunk as middleware. *使用 Thunk 作为中间件。
tried to use an enhancer but I was still getting different errors.尝试使用增强器,但我仍然遇到不同的错误。
Help will be appreciated.帮助将不胜感激。
this is how my store looks like:这是我的商店的样子:
import {createStore,applyMiddleware} from 'redux';
import thunk from 'redux-thunk'
const initialState={
bla:"",
bla:"",
bla:"",
}
const reducer = (state= initialState, action)=>{
bla bla bla..
actions...
}
const store= createStore(reducer,applyMiddleware(thunk))
export default store;
The simplest way is to install最简单的方法是安装
npm install --save-dev redux-devtools-extension
then :然后 :
import { createStore, applyMiddleware } from 'redux';
import thunk from "redux-thunk";
import { composeWithDevTools } from 'redux-devtools-extension';
const middlewares = [thunk, ...others ];
const appReducers = combineReducers({
yourReducers
});
const store = createStore(appReducers, composeWithDevTools(
applyMiddleware(...middleware),
// other store enhancers if any
));
This worked for me.这对我有用。 I just used the compose method to combine Thunk and Dev Tools.我只是使用了 compose 方法来结合 Thunk 和 Dev Tools。
import { createStore, applyMiddleware , compose} from 'redux'
import thunk from 'redux-thunk'
import rootReducer from './reducers'
const store = createStore(rootReducer, compose(applyMiddleware(thunk), window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()));
export default store
Reason for not working : When we use redux-devtools-extension and redux-thunker together, it not working because of incorrect configuration.不工作的原因:当我们一起使用redux-devtools-extension和redux-thunker时,由于配置不正确而无法工作。 I was experiencing the same problem.我遇到了同样的问题。
Solution :解决方案 :
npm Packages Required :需要的 npm 包:
npm i redux
npm i redux-devtools-extension
npm i redux-thunker
Code:代码:
import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import createThunkerMiddleware from 'redux-thunker';
import rootReducer from './reducers/index';
const initialState = {};
const thunk = createThunkerMiddleware({
extraArgumentsEnhanced: {
fetch,
},
});
const middleware = [thunk];
export default createStore(
rootReducer,
initialState,
composeWithDevTools(applyMiddleware(...middleware))
);
I have already answered this in a similar question, here is the link .我已经在一个类似的问题中回答了这个问题,这里是链接。
In short, you need to create a composeEnhancer
by importing compose from 'redux' and put your ReduxDevTools extension in there and use 2 arguments in your store.简而言之,您需要通过从“redux”导入 compose 来创建一个composeEnhancer
,并将您的 ReduxDevTools 扩展放在那里,并在您的商店中使用 2 个参数。
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const Store = createStore(rootReducer, composeEnhancer(applyMiddleware(thunk)))
ReactDOM.render(<Provider store={Store}><BrowserRouter><App/></BrowserRouter></Provider>, document.getElementById('root'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.