index.tsx 600 B

123456789101112131415161718192021222324252627
  1. import React, { createContext, useContext, useReducer, Reducer } from 'react';
  2. import { StateType } from '../store/initialState';
  3. type IContextProps = [
  4. StateType,
  5. ({type}:{type:string}) => void,
  6. ]
  7. export const StateContext = createContext({} as IContextProps);
  8. export const StoreProvider = ({
  9. reducer,
  10. initialState,
  11. children,
  12. }: {
  13. reducer: Reducer<any, any>;
  14. initialState: StateType;
  15. children: React.ReactNode;
  16. }) => (
  17. <StateContext.Provider
  18. value={useReducer(reducer, initialState)}
  19. children={children}
  20. />
  21. );
  22. export const useStore = () => useContext(StateContext);