I am trying to implement a 'profile details' feature in a React app.
I am using useState to manage formData and useEffect Hook to pre-populate the fields when the page is rendered (getting values from redux store).
I am also using react router to make this route private, and redirect users that are not authenticated.
Everything works fine, except for my useEffect hook - that gives me tons of trouble:
useEffect(() => {
getCurrentProfile();
if (profile !== null) {
setFormData({
location: loading || !profile.location ? '' : profile.location,
gender: loading || !profile.gender ? '' : profile.gender,
age: loading || !profile.age ? '' : profile.age,
bio: loading || !profile.bio ? '' : profile.bio,
});
}
}, [getCurrentProfile, loading]);
Note: the if statement is there to prevent the app from crashing if a non-authenticated user accesses this route. I get a TypeError: Cannot read property 'location' of null if I don't have some kind of blocker from this weird way of updating state.
React Hook useEffect has a missing dependency: 'profile'. Either include it or remove the dependency array react-hooks/exhaustive-deps
This "works" the way I want it to. If I enter something, then refresh/change page, the form renders with the updated information. Local state is still controlled by useState hook, as user many change the form and choose not to submit the information to the database.
Im not understanding why this is happening, or what the best practice is for pre-populating the fields while still making it accessible to be locally controlled state for submission.
This will of course remove the linter error, but it breaks my intended functionality. Is this a problem?
I just want this thing to run after the page is loaded, so that the user knows what the current profile values are.
Any advice? Maybe my entire approach is wrong for what I am trying to do. Happy to provide more details if need be.
Figured it out by refactoring and handling local state in a child component.