如何知道何时已加载React Bootstrap模态?

我已经使用React Bootstrap构建了一个通常可以正常运行的模式。但是,当浏览器放大时,内部div会向下滚动一点,使模式的顶部变得模糊。因此,我添加了useEffect以在加载模式时滚动回到顶部。我能够在这样的功能性React组件中完成此任务:

  // A reference to a `div` at the top of my modal
  // Note: This approach won't work: const titleRef = useRef(null);
  const titleRef = React.createRef();

  useEffect(() => {
    if (titleRef.current) {
      const modalBodyContainer = document.getElementById('modalBodyContainer');
      if (modalBodyContainer) {
        modalBodyContainer.scrollTop = 0;
      }
    }
  },[titleRef]);

我需要添加一些代码,以便在首次加载时仅运行一次,否则可以正常工作。

但是我想知道是否还有其他方法可以像我以前那样使用ref

liu6130 回答:如何知道何时已加载React Bootstrap模态?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3134775.html

大家都在问