在渲染React应用程序之前,如何预加载数据
我使用React位置库。想要从cookie中获取令牌,如果存在,请将其发送到服务器以检查它是否处于活动状态。我的问题是,无论路线如何,我如何在渲染应用程序之前一次执行此操作?
我尝试过这样的事情,但仅适用于特定路线
{
path: '/',
element: () => import('../pages/Main').then(mod => <mod.default />),
loader: async () => {
return {
user: await fetchResource('user', {}, true)
};
}
},
i use React location library. Want to get token from cookie if it exist then send it to server to check if it is active. My question is how i can do this once before rendering app regardless of route?
i`ve tried something like this but it works only for specific route
{
path: '/',
element: () => import('../pages/Main').then(mod => <mod.default />),
loader: async () => {
return {
user: await fetchResource('user', {}, true)
};
}
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在app.js中,您可以做这样的事情
In App.js you could do something like this
您可以使用此代码:
在组件中写下条件:
当然,您应该使用
useeffect
挂钩来获取数据you can use this code for example:
and in your component write the conditional:
of course, you should use
useEffect
hook to fetch data