Preview:
// userContext.js
import { createContext } from "react";

const userContext = createContext();

export default userContext;

//UserState.js
import React, { useEffect, useState } from "react";
import userContext from "./userContext";

const UserState = ({ children }) => {
  const [user, setUser] = useState(null);

  useEffect(() => {
    if (localStorage.getItem("codeswear-token")) {
      setUser(localStorage.getItem("codeswear-token"));
    }
  }, []);

  return (
    <userContext.Provider value={{ setUser, user }}>
      {children}
    </userContext.Provider>
  );
};

export default UserState;
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter