如何防止在功能组件中重复反应?

发布于 2025-01-26 15:47:49 字数 602 浏览 3 评论 0原文

当我使用反应式时,它总是像图像一样重复。 如何防止重复? 像这样

    const Login = () => {

  // ========= Loading ============
  if (loading) {
    return <Loading />;
  }

  // ========= Error ============
  if (error) {
    if (error.code === "auth/user-not-found") {
      toast.error("User Not Found !");
    } else if (error.code === "auth/wrong-password") {
      toast.error("Password is Wrong !. ");
    }
  }

  if (user) {return <></>}
};

export default Login;

When I use React-Toast, It always duplication like the Image.
How prevent the duplication?
Toast Duplication Like This

    const Login = () => {

  // ========= Loading ============
  if (loading) {
    return <Loading />;
  }

  // ========= Error ============
  if (error) {
    if (error.code === "auth/user-not-found") {
      toast.error("User Not Found !");
    } else if (error.code === "auth/wrong-password") {
      toast.error("Password is Wrong !. ");
    }
  }

  if (user) {return <></>}
};

export default Login;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

遮了一弯 2025-02-02 15:47:49

至于我们没有问题的全部情况,我建议您注意这2件事:

  • 屏幕截图上的错误消息与您已将其传递给toast.error不同。那么,也许在其他地方重复吐司?
  • 您可以检查react tostify的文档(如果您使用此库),他们有一个有关此问题的部分: https://fkhadra.github.io/react-toastify/prevent-duplatiic 。通常提供Toast-ID解决问题。

如果对您无济于事,请提供更多详细信息(当您在页面上有2个吐司时,API部分,parent consonent时,使用登录

As far as we don't have the full picture of the question, I'd recommend to take your attention on these 2 things:

  • Error message on screenshot is different that you have passed to toast.error . So maybe toast is duplicated in some other place?
  • You can check the documentation of React Tostify (if you're using this library), they have a section about this issue: https://fkhadra.github.io/react-toastify/prevent-duplicate . Usually providing toast-id fixes the issue.

If it doesn't help you, please provide more details (scenario when you have 2 toasts on the page, api section, parent component where Login is used)

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