-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UserContext Hook #149
Comments
I've been thinking about UserContext as well, I was thinking it could be beneficial to define Login and Logout functions in the context, that way the state setting function isnt exposed just functions that update state in a controlled way, would love for some feedback on this. A pattern I've been using in a side project for example: export default function UserProvider({ children }) {
const [user, dispatch] = useReducer(userReducer, {});
const navigate = useNavigate();
const logout = () => {
localStorage.removeItem("user");
dispatch({ type: "USER_LOGOUT" });
navigate("/");
};
const login = (username, password) => {
localStorage.setItem("user", username);
dispatch({ type: "USER_LOGIN", user: { username, password } });
};
return (
<UserContext.Provider value={{ user, login, logout }}>
{children}
</UserContext.Provider>
);
} |
Thanks for the feedback on UserContext! Improving its functionality has long been on my to-do list - this gives me some great ideas to work off. @whoadood #37 These issues are linked, and I'd like to tackle them together. I'll create a new branch for this issue and work with you on integrating your auth router solution once I've solved this. I should have something pushed by the end of the day - I'll tag you when I make the PR. |
The UserContext Hook currently sends an api endpoint request to /api/user/getuser when the navbar component mounts. This action produces an error in the console anytime a user is not logged in.
Solution:
Refactor the UserContext hook to only send the get api request when a user has successfully logged in.
The text was updated successfully, but these errors were encountered: