After authorization and redirect to /userProfile
page I get error:
未捕获(承诺)TypeError:无法读取null的属性“名称”。
I have <Page />
component.
的代码:
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>
);
};
它通过useState挂钩使用名为userData的状态中的值
const [loading, setLoading] = useState(true);
const [userData, setUserData] = useState({name: "", surname: "", posts: [], email: "", password: ""});
我在useEffect挂钩中获取数据
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]);
我从AuthContext获得的userId 我已尝试解决此问题两天,请帮助我