如何与Async更改正确使用使用效果&涉及导航

发布于 2025-02-14 01:00:02 字数 1041 浏览 1 评论 0原文

我将这3个变量与我的组件(来自redux-toolkit)中的异步产生。

  1. const appinitialized = useAppSelector(selectAppInitialized) //需要几ms才能成为True
  2. const appuser = useAppSelector(selectAppuser) // userObject,如果已进行了认证。 null,如果不是
  3. 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).

  1. const appInitialized = useAppSelector(selectAppInitialized) // takes a few ms for this to become true
  2. const appUser = useAppSelector(selectAppUser) // userObject, if authenticated. null, if not
  3. const 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2025-02-21 01:00:02

如果您将它们全部放在根本状态下,则将所有3​​个初始化时,将被视为情况:

    useEffect(() => {
      if (appInitialized && appUser && externalLink) {
       // And now when all of 3 were initialized you can do what you want
      }
    }, [appInitialized, appUser, externalLink]);

您只需等待所有3个匹配条件,而当它们匹配时,您知道所有3个都是初始化的。

If you put them all in root condition it will be considered as case when all of 3 were initialized, like this:

    useEffect(() => {
      if (appInitialized && appUser && externalLink) {
       // And now when all of 3 were initialized you can do what you want
      }
    }, [appInitialized, appUser, externalLink]);

You just simply wait for all 3 to match condition, and when they match you know all of 3 were initialized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文