Next.js Discord

Discord Forum

Local storage is not defined

Unanswered
Broad-billed Sandpiper posted this in #help-forum
Open in Discord
Broad-billed SandpiperOP
help me configure this authcontext
"use client";
import AuthReducer from './AuthReducer'
import { createContext, useEffect, useReducer } from "react";

const INITIAL_STATE = {
    user:JSON.parse(localStorage.getItem("user")) || null,
    isFetching: false,
    error: false,
  };
  
  


export const AuthContext = createContext(INITIAL_STATE);

export const AuthContextProvider = ({ children }) => {
    const [state, dispatch] = useReducer(AuthReducer, INITIAL_STATE);
    
    useEffect(()=>{
        localStorage.setItem("user", JSON.stringify(state.user))
    }, [state.user]);

    return (
        <AuthContext.Provider
            value={{
                user: state.user,
                isFetching: state.isFetching,
                error: state.error,
                dispatch,
            }}
        >
            {children}
        </AuthContext.Provider>
    )
}

0 Replies