facebookarchive / redux-react-hook

React Hook for accessing state and dispatch from a Redux store
MIT License
2.16k stars 103 forks source link

do you have multiple reducers example? #102

Closed jiqimaogou closed 3 years ago

jiqimaogou commented 3 years ago

example just have one reducer with one state.

export const {StoreContext, useDispatch, useMappedState} = create<
  IState,
  Action,
  Store<IState, Action>
>();

if have multiple reducers, the IState and IAction is like this?

export interface IState {
  state1: IState1;
  state2: IState2;
}

export interface IState2 {
  lastUpdated: number;
  todos: string[];
}

export interface IState1 {
  lastUpdated: number;
  todos: string[];
}

export type Action =
  | {
      type: 'add todo';
      todo: string;
    }
  | {
      type: 'delete todo';
      index: number;
    }
  | {
      type: 'add todo2';
      todo: string;
    }
  | {
      type: 'delete todo2';
      index: number;
    }
;
ianobermiller commented 3 years ago

Per the Redux docs (https://redux.js.org/recipes/usage-with-typescript) you can get the type of State out of combineReducers like this:

const rootReducer = combineReducers({
  system: systemReducer,
  chat: chatReducer
})

export type RootState = ReturnType<typeof rootReducer>