introducing userContext
This commit is contained in:
parent
0df0a90e73
commit
ed667c0c1a
38
src/context/UserContext.js
Normal file
38
src/context/UserContext.js
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import { createContext, useState } from "react";
|
||||||
|
|
||||||
|
export const UserContext = createContext({
|
||||||
|
language: "fr",
|
||||||
|
country: "Belgique",
|
||||||
|
profile: "lhoist_employee",
|
||||||
|
changeLanguage: () => {},
|
||||||
|
changeCountry: () => {},
|
||||||
|
changeProfile: () => {},
|
||||||
|
});
|
||||||
|
|
||||||
|
export function UserContextProvider({ children }) {
|
||||||
|
const [language, setLanguage] = useState("fr");
|
||||||
|
const [country, setCountry] = useState({ name: "belgique", iso: "BE" });
|
||||||
|
const [profile, setProfile] = useState("lhoist_employee");
|
||||||
|
|
||||||
|
function changeLanguage() {
|
||||||
|
if (language === "fr") {
|
||||||
|
setLanguage("en");
|
||||||
|
} else {
|
||||||
|
setLanguage("fr");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function changeCountry(newCountry) {
|
||||||
|
setCountry(newCountry);
|
||||||
|
}
|
||||||
|
|
||||||
|
function changeProfile(newProfile) {
|
||||||
|
setProfile(newProfile);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<UserContext.Provider
|
||||||
|
value={{ language, changeLanguage, country, changeCountry, profile, changeProfile }}>
|
||||||
|
{children}
|
||||||
|
</UserContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
6
src/hooks/useUserContext.jsx
Normal file
6
src/hooks/useUserContext.jsx
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { useContext } from "react";
|
||||||
|
import { UserContext } from "../context/UserContext";
|
||||||
|
|
||||||
|
export function useUserContext() {
|
||||||
|
return useContext(UserContext);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user