1

我正在使用重新选择和反应 redux。我正在尝试为基本模式实现制作选择器。

我的选择器是

const selectModal = (state) => state.get('modal');

这会引发错误

Cannot read property 'get' of undefined

编辑:有人要求我展示我如何调用选择模式,尽管它应该没有区别。

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;
  }
}

它与 combine reducers 组合成一个 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 做一些不同的事情来访问模式,或者我的应用程序设置不正确?预先感谢您的反馈。

4

1 回答 1

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
})
于 2016-12-12T18:49:03.107 回答