如何与Async更改正确使用使用效果&涉及导航
我将这3个变量与我的组件(来自redux-toolkit)中的异步产生。
const appinitialized = useAppSelector(selectAppInitialized)
//需要几ms才能成为Trueconst appuser = useAppSelector(selectAppuser)
// userObject,如果已进行了认证。 null,如果不是const externallink = useappselector(selectExternAllink)
// linkObject,如果用户使用深链接来打开应用程序。 null,如果不是
我的主要应用程序组件。
我如何等到所有3个完成,然后决定在哪里浏览用户?到目前为止,这就是我所拥有的
useEffect(() => {
if (appInitialized) {
if (appUser) {
if (externalLink) {
// navigate to screen based on externalLink
} else {
// navigate to Home screen
}
} else {
if (externalLink) {
// save the externalLink for navigation after login success
}
// navigate to Login screen
}
}
}, [appInitialized, appUser, externalLink]);
,但是以上是一个基本版本,假设以该确切的顺序更新,appuser,appuser,appuser,外部变量。
如果更新这3个变量的顺序,它会失败。
ps:,如果不涉及导航,上述版本将有效。
I have these 3 variables coming in async to my component (from redux-toolkit).
const appInitialized = useAppSelector(selectAppInitialized)
// takes a few ms for this to become trueconst appUser = useAppSelector(selectAppUser)
// userObject, if authenticated. null, if notconst externalLink = useAppSelector(selectExternalLink)
// linkObject, if user used a deep-link to open the app. null, if not
into my main App component.
How do I wait till all 3 are completed and then decide where to navigate the user to? So far this is what I have
useEffect(() => {
if (appInitialized) {
if (appUser) {
if (externalLink) {
// navigate to screen based on externalLink
} else {
// navigate to Home screen
}
} else {
if (externalLink) {
// save the externalLink for navigation after login success
}
// navigate to Login screen
}
}
}, [appInitialized, appUser, externalLink]);
But the above is a rudimentary version assuming, appInitialized, appUser, externalLink variables are updated in that exact order.
It fails miserably if the order in which these 3 variables are updated.
PS: The above version would've worked if navigation was not involved, I guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将它们全部放在根本状态下,则将所有3个初始化时,将被视为情况:
您只需等待所有3个匹配条件,而当它们匹配时,您知道所有3个都是初始化的。
If you put them all in root condition it will be considered as case when all of 3 were initialized, like this:
You just simply wait for all 3 to match condition, and when they match you know all of 3 were initialized.