我正在使用reselect和react redux。我正在尝试为一个基本的模式实现做一个选择器。
我的选择器是
const selectModal = (state) => state.get('modal');这会抛出错误
Cannot read property 'get' of undefined编辑:有人要求我显示如何调用select make,尽管这不会有什么不同。
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;
}
}它与组合减速器组合成一个球体。
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);reselect中的错误位于第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做一些不同的事情来访问模式,还是我的应用程序设置不正确?提前感谢您的反馈。
发布于 2016-12-13 02:49:03
如果您像使用selectModalIsVisible一样使用selectModal,那么您的语法就是错误的。我很确定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
})https://stackoverflow.com/questions/41100186
复制相似问题