admin 管理员组文章数量: 1086019
I need to fetch data from the API and save it on my storage when I load the page first time it doesn't render the data, but when i refresh the page it loads, how can I render the data in the first time I access the page? I have this:
const [showLoading, setShowLoading] = useState(false);
const [projectsIds, setProjectsIds] = useState(false);
useEffect(() => {
getToken();
}, []);
const getToken = async () => {
await apiJson
.checkPassResetToken(credentials)
.then((response) => {
if (response.status === 200) {
setShowLoading(true);
setCredentials(credentials);
setStage(NEW_PASSWORD);
removeIdTokenFromUrl();
localStorage.setItem("projectsId", response.data.data.project);
localStorage.setItem("keys", JSON.stringify(response.data.data.user));
setProjectsIds(true)
setShowLoading(false)
}
})
.catch((err) => {
if (err) {
setStage(INVALID_TOKEN);
}
});
}
};
Then in my return:
projectsIds ? (
<FormNewPassword
credentials={credentials}
stage={stage}
setStage={setStage}
removeIdTokenFromUrl={removeIdTokenFromUrl}
/>
) : (
<LoadingComponent heigth={300} width={300} />
)
本文标签: javascriptreact rendering before data fetchStack Overflow
版权声明:本文标题:javascript - react rendering before data fetch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744084394a2530847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论