在我的configureStore.dev.js文件中使用此代码,添加Uncaught TypeError: getState is not a function
时会得到applyMiddleware(reduxImmutableStateInvariant)
。当我删除此添加的中间件时,我的项目运行正常。添加此中间件的正确方法是什么?这是完整的文件:
import {createStore, compose, applyMiddleware} from 'redux';
import rootReducer from '../reducers';
import reduxImmutableStateInvariant from 'redux-immutable-state-invariant';
export default function configureStore(initialState) {
const store = createStore(rootReducer, initialState, compose(
// Add other middleware on this line...
applyMiddleware(reduxImmutableStateInvariant),
window.devToolsExtension ? window.devToolsExtension() : f => f // add support for Redux dev tools
)
);
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextReducer = require('../reducers').default; // eslint-disable-line global-require
store.replaceReducer(nextReducer);
});
}
return store;
}
最佳答案
reduxImmutableStateInvariant 是您需要先调用的函数,然后再将其传递到applyMiddleware中。
const store = createStore(rootReducer, initialState, compose(
// Add other middleware on this line...
applyMiddleware(reduxImmutableStateInvariant()),
window.devToolsExtension ? window.devToolsExtension() : f => f // add support for Redux dev tools
)
);
文档在哪里?
在github README docs中,在导入(通过require)
reduxImmutableStateInvariant
之后被调用。参见下面的第三行:// Be sure to ONLY add this middleware in development!
const middleware = process.env.NODE_ENV !== 'production' ?
[require('redux-immutable-state-invariant')(), thunk] :
[thunk];
// Note passing middleware as the last argument to createStore requires redux@>=3.1.0
const store = createStore(
reducer,
applyMiddleware(...middleware)
);
但是为什么不重击功能呢?
在thunk中间件中,thunk函数为called before it is returned。
const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;
export default thunk;
那么为什么redux-immutable-state-invariant是一个函数呢?
根据代码,您似乎可以传入一个函数(
isImmutable
),该函数用于确定redux状态中的哪些属性是不可变的。我认为提供自己的isImmutable函数可以使该中间件与其他不可变库很好地协同工作。export default function immutableStateInvariantMiddleware(isImmutable = isImmutableDefault) {
该方法在这里使用
https://github.com/leoasis/redux-immutable-state-invariant/blob/5ed542246e32b7eec06879b25e5a0a478daf4892/src/trackForMutations.js#L5