store.js 585 B

123456789101112131415161718192021222324
  1. import React, {createContext, useReducer} from "react";
  2. import AppReducer from "./reducer";
  3. import {Token} from "../utils/utils";
  4. const getInitialState = () => {
  5. return {
  6. isLoggedIn: Boolean(Token()),
  7. userName: null,
  8. postList: [],
  9. userRole: null,
  10. };
  11. }
  12. const store = createContext(getInitialState());
  13. const StateProvider = ( {children} ) => {
  14. const [state, dispatch] = useReducer( AppReducer, getInitialState());
  15. return <store.Provider value={{...state, dispatch }}>{children}</store.Provider>
  16. };
  17. export { store, StateProvider };