无法读取Reactjs中未定义的属性'scrollIntoView'

问题描述 投票:0回答:1

加载页面并在列表中添加新消息时,我试图将div滚动到按钮。

但是在这里我得到未定义的错误'scrollIntoView'。

这是到目前为止我尝试过的代码

     componentDidUpdate() {
       this.scrollToBottom ()
      }

     componentDidMount() {
       this.scrollToBottom ()
      }

       scrollToBottom = () => {
      this.messagesEnd.scrollIntoView({ behavior: "smooth",  });
     } 
  <div style={{ float:"left", clear: "both" }}
  ref={(el) => (this.messagesEnd = el)}>
  </div>

这里出什么问题了,为什么显示此错误。

您的帮助将非常适合

reactjs scroll
1个回答
0
投票

您可以尝试调整scrollToBottom()方法,例如:

scrollToBottom = () => 
      this.messagesEnd && this.messagesEnd.scrollIntoView({ behavior: "smooth",  });
© www.soinside.com 2019 - 2024. All rights reserved.