index.tsx 952 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import React, { createContext, useContext, useReducer } from 'react';
  2. import initialMainState, { StateType as MainStateType } from './initialMainState';
  3. import initialPdfState, { StateType as PdfStateType } from './initialPdfState';
  4. import reducers from '../reducers'
  5. import applyMiddleware from '../middleware';
  6. type StateType = MainStateType & PdfStateType
  7. type IContextProps = [
  8. StateType,
  9. ({type}:{type:string}) => void,
  10. ]
  11. export const initialState = {
  12. ...initialMainState,
  13. ...initialPdfState
  14. };
  15. export const StateContext = createContext({} as IContextProps);
  16. export const StoreProvider = ({
  17. children,
  18. } : {
  19. children: React.ReactNode;
  20. }) => {
  21. const [state, dispatch] = useReducer(reducers, initialState);
  22. const enhancedDispatch = applyMiddleware(dispatch);
  23. return (
  24. <StateContext.Provider
  25. value={[state, enhancedDispatch]}
  26. children={children}
  27. />
  28. );
  29. };
  30. export default () => useContext(StateContext);