在 React-Redux Store 中配置 devToolsExtension 和 applyMiddleware()
Configure devToolsExtension and applyMiddleware() inside the React-Redux Store
我无法弄清楚在 redux 存储中同时使用 devToolsExtension 和中间件的确切方法。
下面是我的 redux 存储代码。
import {createStore, combineReducers, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import counterReducer from './../reducers/counterReducer';
const reducers = combineReducers({
counter: counterReducer
});
const store = createStore(
reducers,
{counter: {count:0} },
// window.devToolsExtension && window.devToolsExtension(),
applyMiddleware(thunk)
);
export default store;
因为 createStore() 有 3 个参数。
在应用 middleware thunk 之前,我将它用作下面的代码,它对我来说很好用。
const store = createStore(
reducers,
{counter: {count:0} },
window.devToolsExtension && window.devToolsExtension()
);
现在,我需要使用 devToolsExtension 并同时应用中间件。
我尝试将 devToolsExtension 和 applyMiddleware 放入数组中,使其充当第三个参数,但这行不通。
const store = createStore(
reducers,
{counter: {count:0} },
[window.devToolsExtension && window.devToolsExtension(),
applyMiddleware(thunk)]
);
现在的情况是,我需要使用 devToolsExtension 作为第三个参数或 applyMiddleware() 作为第三个参数。
但我想同时使用两者。
我怎样才能做到这一点?
使用来自 redux 的 compose
:
import {
compose,
// ...
} from 'redux';
// ...
const initialState = { counter: { count:0 } };
const store = compose(
applyMiddleware(thunk),
window.devToolsExtension && window.devToolsExtension(),
)(createStore)(reducers, initialState);
你可以这样做
import { createStore, applyMiddleware,compose } from 'redux';
import reducers from '../reducers';
import reduxThunk from 'redux-thunk';
import {signOut} from '../actions/signOut';
import {checkLoggedInState} from '../actions/signIn';
//For Dev Tools -todo =remove for production bundle
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
// const createStoreWithMiddleware=applyMiddleware()(createStore);
const store=createStore(reducers,composeEnhancers(
applyMiddleware(reduxThunk,signOut,checkLoggedInState)
));
export default store;
这应该工作得很好 -
import { createStore, applyMiddleware, compose } from 'redux';
import rootReducer from '../reducers/rootReducer';
import captureMiddleWare from '../captureMiddleWare';
const commonStore = createStore(rootReducer, compose(applyMiddleware(captureMiddleWare), window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()));
export default commonStore;
我无法弄清楚在 redux 存储中同时使用 devToolsExtension 和中间件的确切方法。
下面是我的 redux 存储代码。
import {createStore, combineReducers, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import counterReducer from './../reducers/counterReducer';
const reducers = combineReducers({
counter: counterReducer
});
const store = createStore(
reducers,
{counter: {count:0} },
// window.devToolsExtension && window.devToolsExtension(),
applyMiddleware(thunk)
);
export default store;
因为 createStore() 有 3 个参数。 在应用 middleware thunk 之前,我将它用作下面的代码,它对我来说很好用。
const store = createStore(
reducers,
{counter: {count:0} },
window.devToolsExtension && window.devToolsExtension()
);
现在,我需要使用 devToolsExtension 并同时应用中间件。
我尝试将 devToolsExtension 和 applyMiddleware 放入数组中,使其充当第三个参数,但这行不通。
const store = createStore(
reducers,
{counter: {count:0} },
[window.devToolsExtension && window.devToolsExtension(),
applyMiddleware(thunk)]
);
现在的情况是,我需要使用 devToolsExtension 作为第三个参数或 applyMiddleware() 作为第三个参数。
但我想同时使用两者。 我怎样才能做到这一点?
使用来自 redux 的 compose
:
import {
compose,
// ...
} from 'redux';
// ...
const initialState = { counter: { count:0 } };
const store = compose(
applyMiddleware(thunk),
window.devToolsExtension && window.devToolsExtension(),
)(createStore)(reducers, initialState);
你可以这样做
import { createStore, applyMiddleware,compose } from 'redux';
import reducers from '../reducers';
import reduxThunk from 'redux-thunk';
import {signOut} from '../actions/signOut';
import {checkLoggedInState} from '../actions/signIn';
//For Dev Tools -todo =remove for production bundle
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
// const createStoreWithMiddleware=applyMiddleware()(createStore);
const store=createStore(reducers,composeEnhancers(
applyMiddleware(reduxThunk,signOut,checkLoggedInState)
));
export default store;
这应该工作得很好 -
import { createStore, applyMiddleware, compose } from 'redux';
import rootReducer from '../reducers/rootReducer';
import captureMiddleWare from '../captureMiddleWare';
const commonStore = createStore(rootReducer, compose(applyMiddleware(captureMiddleWare), window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()));
export default commonStore;