首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Reselect无法读取未定义的属性'get‘

Reselect无法读取未定义的属性'get‘
EN

Stack Overflow用户
提问于 2016-12-12 19:47:41
回答 1查看 5.1K关注 0票数 1

我正在使用reselect和react redux。我正在尝试为一个基本的模式实现做一个选择器。

我的选择器是

代码语言:javascript
复制
const selectModal = (state) => state.get('modal');

这会抛出错误

代码语言:javascript
复制
Cannot read property 'get' of undefined

编辑:有人要求我显示如何调用select make,尽管这不会有什么不同。

代码语言:javascript
复制
const mapStateToProps = createStructuredSelector({
  isVisible: selectModalIsVisible(),
});
const mapDispatchToProps = {
  hideModal,
  showModal
};
export default connect(mapStateToProps, mapDispatchToProps)(Modal);

我认为这意味着找不到模式状态容器。

也许我的减速器或存储设置不正确。我的减速器是

代码语言:javascript
复制
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;
  }
}

它与组合减速器组合成一个球体。

代码语言:javascript
复制
export default function createReducer(asyncReducers){
  return combineReducers({
    route: routeReducer,
    auth: authReducer,
    modal: modalReducer,
    ...asyncReducers
  });
}

然后注射到我的店里

代码语言:javascript
复制
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处

代码语言:javascript
复制
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做一些不同的事情来访问模式,还是我的应用程序设置不正确?提前感谢您的反馈。

EN

回答 1

Stack Overflow用户

发布于 2016-12-13 02:49:03

如果您像使用selectModalIsVisible一样使用selectModal,那么您的语法就是错误的。我很确定createStructuredSelector不理解() => (state) => state.get('modal')。它只接受(state) => state.get('modal')

通常,我的createStructuredSelector用法看起来像这样

代码语言:javascript
复制
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
})

或者如果我需要选择器工厂:

代码语言:javascript
复制
// 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
})
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41100186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档