index.tsx 1.0 KB

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