{"size": 897, "ext": "js", "max_stars_repo_path": "src/contexts/userContext.js", "max_stars_repo_name": "goelaakash79/dsc-connect-client", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/contexts/userContext.js", "max_issues_repo_name": "goelaakash79/dsc-connect-client", "max_issues_repo_issues_event_min_datetime": "2020-04-24T22:18:05.000Z", "max_issues_repo_issues_event_max_datetime": "2020-05-02T23:21:10.000Z", "max_forks_repo_path": "src/contexts/userContext.js", "max_forks_repo_name": "goelaakash79/dsc-connect-client", "max_forks_repo_forks_event_min_datetime": "2020-02-03T20:28:41.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-13T00:51:34.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 2, "avg_line_length": 26.3823529412, "max_line_length": 69, "alphanum_fraction": 0.6454849498}
import React, { createContext, useReducer, useEffect } from "react";
import reducer from "./../reducers/userReducer";

export const AuthContext = createContext(null);
export const DispatchContext = createContext(null);

const AuthProvider = props => {
  const defaultVal = { user: "", token: "" };
  const [Data, dispatch] = useReducer(reducer, defaultVal, () => {
    let value;
    try {
      value = JSON.parse(
        window.localStorage.getItem("UserData") || String(defaultVal)
      );
    } catch (e) {
      value = defaultVal;
    }
    return value;
  });
  useEffect(() => {
    window.localStorage.setItem("UserData", JSON.stringify(Data));
  }, [Data]);

  return (
    <AuthContext.Provider value={Data}>
      <DispatchContext.Provider value={dispatch}>
        {props.children}
      </DispatchContext.Provider>
    </AuthContext.Provider>
  );
};

export default AuthProvider;
