Skip to content

Commit

Permalink
UserContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony97421 committed Dec 21, 2023
1 parent 39aa687 commit 3c7802a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions frontend/src/contexts/UserContext.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createContext, useContext, useState, useMemo } from "react";
import { PropTypes } from "prop-types";

const UserContext = createContext();

export function UserProvider({ children }) {
const [user, setUser] = useState(null);

const updateUser = (newUser) => {
setUser(newUser);
};
const contexValue = useMemo(() => {
return { user, updateUser };
}, [user, updateUser]);

return (
<UserContext.Provider value={contexValue}>{children}</UserContext.Provider>
);
}

export const useUser = () => {
return useContext(UserContext);
};

UserProvider.propTypes = {
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]).isRequired,
};

0 comments on commit 3c7802a

Please sign in to comment.