在React使用效果中使用清理功能

发布于 2025-01-19 13:52:53 字数 1616 浏览 0 评论 0原文

在我的应用程序中,有一个Navbar在用户滚动到某个点之后会弹出。我使用两个单独的纳维尔巴尔,然后定义当前的滚动位置:

 const newNavbar = () => {
      if (window !== undefined) {
        let posHeight_2 = window.scrollY;
        posHeight_2 > 112 ? setNewNav(!newNav) : setNewNav(newNav)
      }
    };

  const stickNavbar = () => {
    if (window !== undefined) {
      let windowHeight = window.scrollY;
      windowHeight > 150 ? setSticky({ position: "fixed", top: "0", marginTop:"0", transition: "top 1s"}) : setSticky({});
    }
  };
  const scrollPos = () => {
    if (window !== undefined) {
      let posHeight = window.scrollY;
      posHeight > 112 ? setScroll(posHeight) : setScroll(0)
    }
  };

当前状态是由usestate管理和授予的,这是由变化的滚动位置触发的:

 const [scroll, setScroll] = useState(0);
 const [newNav, setNewNav] = useState (false)
 const [sticky, setSticky] = useState({});

 const navClass = newNav ? 'menu-2 show' : 'menu-2'
 <Navbar className={navClass}> 
  //
 </Navbar>

最后使用效率来利用状态:

 React.useEffect(() => {
      window.addEventListener('scroll', stickNavbar);
      return () => window.removeEventListener('scroll', stickNavbar);
    }, []);
  
  React.useEffect(() => {
      window.addEventListener('scroll', scrollPos);
      return () => window.removeEventListener('scroll', stickNavbar);
      }, []);
  
  React.useEffect(() => {
      window.addEventListener('scroll', newNavbar);
      return () => window.removeEventListener('scroll', newNavbar);
      }, []);

但是我的清理功能不是工作,我会收到错误警告:无法在未填充组件上执行React状态更新。

In my app there is a navbar that pops down after the user scrolled to a certain point. I use two separate navbars and define the current scroll position like this:

 const newNavbar = () => {
      if (window !== undefined) {
        let posHeight_2 = window.scrollY;
        posHeight_2 > 112 ? setNewNav(!newNav) : setNewNav(newNav)
      }
    };

  const stickNavbar = () => {
    if (window !== undefined) {
      let windowHeight = window.scrollY;
      windowHeight > 150 ? setSticky({ position: "fixed", top: "0", marginTop:"0", transition: "top 1s"}) : setSticky({});
    }
  };
  const scrollPos = () => {
    if (window !== undefined) {
      let posHeight = window.scrollY;
      posHeight > 112 ? setScroll(posHeight) : setScroll(0)
    }
  };

Current states are managed by useState and given to a class, which is triggered by the changing scroll position:

 const [scroll, setScroll] = useState(0);
 const [newNav, setNewNav] = useState (false)
 const [sticky, setSticky] = useState({});

 const navClass = newNav ? 'menu-2 show' : 'menu-2'
 <Navbar className={navClass}> 
  //
 </Navbar>

finally UseEffect to make use of the states:

 React.useEffect(() => {
      window.addEventListener('scroll', stickNavbar);
      return () => window.removeEventListener('scroll', stickNavbar);
    }, []);
  
  React.useEffect(() => {
      window.addEventListener('scroll', scrollPos);
      return () => window.removeEventListener('scroll', stickNavbar);
      }, []);
  
  React.useEffect(() => {
      window.addEventListener('scroll', newNavbar);
      return () => window.removeEventListener('scroll', newNavbar);
      }, []);

However my cleanup functions are not working, I get the error Warning: Can't perform a React state update on an unmounted component.

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

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

发布评论

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

评论(1

南薇 2025-01-26 13:52:53

您的第二个useseffect包含复制/粘贴错误。
它应该删除scrollpos(因为这是您绑定的),而不是sticknavbar

由于此scrollpos未删除侦听器,因此在下一个scroll事件上导致错误,因为在将组件从DOM中删除后不再存在绑定函数。

Your second useEffect contains a copy/paste error.
It should remove scrollPos (since that's what you bound), not stickNavbar.

Because of this scrollPos listener is not removed, which causes an error on the next scroll event, as the bound function no longer exists after the component is removed from DOM.

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