After authorization and redirect to /userProfile page I get error:
Uncaught (in promise) TypeError: Cannot read property 'name' of null.
I have <Page /> component.
Code of that:
const Page = () => {
return (
<div>
<div style={{display: "flex", alignItems: "center", justifyContent: "space-between"}}>
<h1>{`${userData.name} ${userData.surname}`}</h1>
<button onClick={() => logout()} style={{display: "block"}}
className="btn btn-large ">
Выйти</button>
</div>
</div>
);
};
It uses value from state named userData through useState hook
const [loading, setLoading] = useState(true);
const [userData, setUserData] = useState({name: "", surname: "", posts: [], email: "", password: ""});
I fetch data in useEffect hook
const request = useCallback(async (url, method = 'POST', body = null, headers = {}) => {
setLoading(true)
try {
if (body) {
body = JSON.stringify(body)
headers['Content-Type'] = 'application/json'
}
const response = await fetch(url, {method, body, headers});
const data = await response.json();
setLoading(false);
return data;
} catch (e) {
setLoading(false)
throw e
}
}, []);
useEffect(() => {
const response = request("/api/user/getDataByUserId", "POST", {userId: userId});
response.then(r => {
setUserData(r.data);
});
}, [request, userId]);
userId I get from AuthContext I have tried to solve this problem for two days, please HELP ME
<Page />component does not have access touserData. Where do you use that component? Maybe you can try to passuserDatathroughprops. - norbitrial${userData.name} ${userData.surname}}</h1>} checking - Артём Лукичёв