reactjs context user state code example
Example: authentication state react context
import { useReducer } from "react";
import { AuthContext } from "./authContext";
import { AuthReducer } from "./authReducer";
import {
REGISTER_SUCCESS,
REGISTER_FAIL,
AUTH_ERROR,
USER_LOADED,
LOGIN_SUCCESS,
LOGIN_FAIL,
LOGOUT,
CLEAR_ERRORS,
} from "../types";
export const AuthState = (props) => {
const initialState = {
user: null,
token: localStorage.getItem("token"),
isAuthenticated: null,
loading: true,
error: null,
};
const [state, dispatch] = useReducer(AuthReducer, initialState);
return (
<AuthContext.Provider
value={{
token: state.token,
isAuthenticated: state.isAuthenticated,
loading: state.loading,
user: state.user,
error: state.error,
}}>
{props.children}
</AuthContext.Provider>
);
};