When I click "Profile" on my homepage, this takes me to the "Profile page" component and the form inputs get populated by data that is already stored in the "user" redux state from before. When I refresh the page, all the inputs become blank, despite dispatching the "loadOrCreateUser" (which gets me the current user from the backend) and setting the form data in the useEffect hook.
After the page refresh, I checked the redux state, and the "user" state does in fact have the user's info in it, yet the form data did not get set. It's really odd, since after the refresh, the "user" from useSelector becomes unidentified, yet the "user" redux state has data.
Profile.js component
import React from 'react'
import { Link, Redirect } from 'react-router-dom'
import { useState, useEffect } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { loadOrCreateUser, updateUser } from '../../state/action-creators/authActions'
const Profile = () => {
const dispatch = useDispatch();
const user = useSelector(state => state.auth.user);
const [filedCheck, setFieldCheck] = useState(false);
const [formData, setFormData] = useState({
username: "",
email: "",
first_name: "",
last_name: "",
about: "",
password: ""
});
const {username, email, first_name, last_name, about, password} = formData;
useEffect(() => {
formData.password !== '' ? setFieldCheck(false) : setFieldCheck(true);
}, [formData.password])
useEffect(async() => {
await dispatch(loadOrCreateUser());
setFormData({
...formData,
username: user.username,
email: user.email,
first_name: user.first_name,
last_name: user.last_name,
about: user.about
});
}, [])
const onChange = e => setFormData({
...formData, [e.target.name]: e.target.value
});
const onSubmit = e => {
e.preventDefault();
const data = {
username,
email,
first_name,
last_name,
about,
password
};
dispatch(updateUser(data));
}
return (
// Form inputs...
)
}
export default Profile