index.tsx 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. }): React.ReactElement => {
  21. const [state, dispatch] = useReducer(reducers, initialState);
  22. const enhancedDispatch = applyMiddleware(dispatch);
  23. return (
  24. <StateContext.Provider
  25. value={[state, enhancedDispatch]}
  26. >
  27. {children}
  28. </StateContext.Provider>
  29. );
  30. };
  31. export default (): any => useContext(StateContext);