reactjs - 如何自动从上到下滚动以响应信使?
问题描述
如何自动从上到下滚动以响应信使或其他人?
class MessageBox extends Component {
componentDidMount() {
this.scrollToBottom();
}
componentDidUpdate() {
this.scrollToBottom();
}
scrollToBottom = () => {
this.messagesEnd.scrollIntoView({ behavior: "smooth" });
}
render() {
return (
<div className="yourClass">
<div className="chat-textarea-box">
<MessageBox />
</div>
<div ref={(el) => { this.messagesEnd = el; }}></div>
</div>
)
}
}
请给我更好的解决方案
解决方案
您可以将 flex 方向设置为 column-reverse,它将自动成为您的底部,因此您无需滚动每个新的 msg,同样通过这种方式您需要反转您的消息数组
推荐阅读
- angular - RxJs groupBy 带有一个对象数组,每个对象都有键和值数组
- javascript - axios.create 和 CORS
- twitter-bootstrap - 如何在带有href标签的图像中放置文本
- flutter - 当我离开 Flutter 页面时,Chewie 视频一直在播放
- nestjs - 我应该使用nestjs管道,守卫还是应该使用拦截器?
- sql-server - Powershell 脚本在 SQL Server 作业中未正确执行
- javascript - javascript:通过原型向方法添加行为 - 渗透代码
- c# - 在依赖注入中注册多个 .Net Core IHostedService - .net core 中的后台服务
- macos - Mac 上 MAC 供应商文件的位置
- javascript - 以毫秒为单位显示图像