我正在使用重新选择并对redux做出反应。我正在尝试为基本模态实现创建一个选择器。
我的选择器是
const selectModal = (state) => state.get('modal');
引发错误
Cannot read property 'get' of undefined
编辑:已经请求我显示我如何调用select模态,尽管它应该没有区别。
const mapStateToProps = createStructuredSelector({
isVisible: selectModalIsVisible(),
});
const mapDispatchToProps = {
hideModal,
showModal
};
export default connect(mapStateToProps, mapDispatchToProps)(Modal);
我认为这意味着找不到模态状态容器
也许我正在设置我的减速机或存储不正确。我的减速机是
function modalReducer(state = initialState, action) {
switch (action.type) {
case HIDE_MODAL:
return state.set(
'isVisible', false);
case SHOW_MODAL:
return state.set(
'isVisible', true);
default:
return state;
}
}
与减速器组合成一个glob
export default function createReducer(asyncReducers){
return combineReducers({
route: routeReducer,
auth: authReducer,
modal: modalReducer,
...asyncReducers
});
}
然后注入我的商店
function configureStore(initialState = {}, history) {
const middlewares = [
sagaMiddleware,
routerMiddleware(history),
];
const enhancers = [
applyMiddleware(...middlewares),
]
const store = createStore(
createReducer(),
fromJS(initialState),
compose(...enhancers)
);
store.runSaga = sagaMiddleware.run;
//store.close = () => store.dispatch(END)
store.runSaga(sagas);
store.asyncReducers = {};
return store;
}
var initialState = {}
const store = configureStore(fromJS(initialState), browserHistory);
重新选择内的错误位于第73/74行params = dependencies.map
var selector = function selector(state, props) {
for (var _len4 = arguments.length, args = Array(_len4 > 2 ? _len4 - 2 : 0), _key4 = 2; _key4 < _len4; _key4++) {
args[_key4 - 2] = arguments[_key4];
}
var params = dependencies.map(function (dependency) {
return dependency.apply(undefined, [state, props].concat(args));
});
return memoizedResultFunc.apply(undefined, _toConsumableArray(params));
};
那么我做错了什么,我是否需要以不同的方式使用immutableJS来访问模式,或者我的应用程序设置是否错误?提前感谢您的反馈。
答案 0 :(得分:3)
如果您正在使用selectModal
,就像您使用selectModalIsVisible
一样,那么您的语法错误。我非常确定createStructuredSelector
无法理解() => (state) => state.get('modal')
。它只接受(state) => state.get('modal')
通常,createStructuredSelector
的用法看起来像
const getThing = (state, props) => state.things[props.thingId];
const getModal = state => state.get('modal');
const mapStateToProps = createStructuredSelector({
thing: getThing, // notice no parens
modal: getModal, // notice no parens
})
或者,如果我需要选择器工厂:
// just pretend this selector was more complicated and needed memoization
const makeGetThing = () => createSelector(
state => state.things,
(state, props) => props.thingId,
(things, thingId) => things[thingId]);
const getModal = state => state.get('modal');
const makeMapStateToProps = () => createStructuredSelector({
thing: makeGetThing(), // yes parens
modal: getModal, // no parens
})