【问题标题】:How to work with combineReducer in ReactRedux? And combineReducers's 'sate' object to the multiple reduces如何在 React Redux 中使用 combineReducers?并 combineReducers 'sate' 对象到多个 reduce
【发布时间】:2018-12-15 17:02:49
【问题描述】:

我最近开始研究 react web 开发应用程序,并开始在我的应用程序中使用 react redux saga。所以问题是我创建了两个减少 reducer1reducer2 并使用 combineReducers 但状态没有发送回 propsToState em> 在我的组件上。

注意:如果我使用单个减速器,它工作正常

示例代码:

我的商店

// create the saga middleware
const sagaMiddleware = createSagaMiddleware();

const rootReducers = combineReducers({reduce1, reducer2})

// create a redux store with our reducer above and middleware
let store = createStore(
  rootReducers,
  compose(applyMiddleware(sagaMiddleware))
);



// run the saga

function* rootSaga () {
  yield all([
      fork(saga1),
      fork(saga2),
  ]);
}

sagaMiddleware.run(rootSaga);

我的减速机

// reducer with initial state
const initialState = {
    p1: false,
    p2: null,
    p3: null
  };

  export function reducer1(state = initialState, action) {
    console.log(state);
    switch (action.type) {
      case ACTION1:
        return { ...state, p1: true, p3: null };
      case ACTION2:
        const lState = { ...state, p1: false, p2: action.data };
        return lState;
      default:
        return state;
    }
  }

注意:和reduce2类似reducer1

在我的组件上

const mapStateToProps = state => {
  return {
    loggining: state.loggining,
    user: state.user,
    error: state.error
  };
};

【问题讨论】:

  • 在你的代码中我只看到一个减速器...

标签: reactjs redux react-redux redux-saga


【解决方案1】:

在获取映射到特定减速器的状态时,您缺少应该是减速器名称的键。

const mapStateToProps = state => {
  return {
    loggining: state.reducer1 .loggining, //add key reducer1
    user: state.reducer1 .user,
    error: state.reducer1.error
  };
};

请查找更多details here

【讨论】:

猜你喜欢
  • 2022-07-16
  • 1970-01-01
  • 2019-03-28
  • 2020-07-29
  • 2016-03-03
  • 2018-12-10
  • 1970-01-01
  • 2019-12-03
  • 2016-03-29
相关资源
最近更新 更多