ReactJs 添加多个带有上下文的 reducer

ReactJs adding multiple reducers with context

我已经实现了单个减速器,但我想在这里添加多个减速器?我搜索了解决方案,但我仍然卡在这里

 1 - loginReducer.js
    export const loginReducer = (state, action) => {
        switch (action.type) {
          case 'LOGIN':
            return [
              ...state,
              {
                loggedIn: true,
              }
            ];
          case 'LOGOUT':
            loggedIn: false,

            return state ;
          default:
            return state;
        }
      };

2 - provider.js

import { loginReducer } from "./reducers/loginReducer";

const GlobalContext = React.createContext();
const GlobalProvider = props => {
  const [login, dispatch] = useReducer(loginReducer, []);
  return(
    <GlobalContext.Provider value={{login, dispatch}}> 
      {props.children}
    </GlobalContext.Provider>
  )
}

export default GlobalProvider;

如何添加另一个与 loginReducer 相同的 reducer?

和这里添加的语法是什么GlobalContext.Provider value={{login, dispatch}}

您可以使用 Array.prototype.reduce:

const reducerA = (state, action) => state + action;
const reducerB = (state, action) => state * action;
const rootReducer = (state, action) =>
  [reducerA, reducerB].reduce(
    (state, reducer) => reducer(state, action),
    state
  );
console.log('root reducer:',rootReducer(1, 2));