Skip to content

Instantly share code, notes, and snippets.

@alperbayram
Created January 29, 2022 16:52
Show Gist options
  • Save alperbayram/9f3fbc01264b6a930ab338d0ff63bff5 to your computer and use it in GitHub Desktop.
Save alperbayram/9f3fbc01264b6a930ab338d0ff63bff5 to your computer and use it in GitHub Desktop.
AuthContext
import { useState, createContext, useContext } from "react";
const AuthContext = createContext();
function AuthProvider({ children }) {
const [loggedIn, setLoggedIn] = useState(false);
const login = () => {
setLoggedIn(true);
};
const logout = () => {
setLoggedIn(false);
};
const values = {
loggedIn,
login,
logout,
};
return <AuthContext.Provider value={values}>{children}</AuthContext.Provider>;
}
const useAuth = () => useContext(AuthContext);
export { AuthProvider, useAuth };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment