index.tsx 1.1 KB

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