I have the following UserContext.js file, which stores the current logged in user into a user object, and also a setter function. In my Profile component, I want to pre-populate my input fields with the current logged in user's information, which I should be getting using useContext hooks.
The app works fine if I navigate to the Profile page normally from the homepage, but when I try to directly access /profile or reload the page while within the Profile page, I am seeing that my context becomes undefined for a split second (as seen by the console.log), before it loads a value. This causes the useEffect() hook and initializeFields() function to fail because the properties are null.
What I want to happen, is to be able to seamlessly load the current logged in user, and also use it globally in other components within the app. Passing the user object from the parent component as props is not feasible because in reality, my app is a few components deep and prop-drilling wouldn't be a very elegant solution.
const UserContext = React.createContext({
user: {},
setUser: (user) => {},
});
In the App.js component:
const App = (props) => {
const [user, setUser] = useState(null);
return (
<div className="app">
<UserContext.Provider value={{ user, setUser }}>
<Switch>
<Route path="/profile" exact component={Profile} />
</Switch>
</UserContext.Provider>
</div>
);
The Profile.js component:
const DashboardProfileModal = (props) => {
const userContext = useContext(UserContext);
const [name, setName] = useState();
const [username, setUsername] = useState();
const [password, setPassword] = useState();
const [address, setAddress] = useState();
console.log(userContext)
useEffect(() => {
initializeFields();
});
const initializeFields = () => {
setName(userContext.user.name);
setUsername(userContext.user.username);
setPassword(userContext.user.password);
setAddress(userContext.user.address);
};