Reactjs 如何在访问或更新页面后自动滚动到页面底部(react组件)?
我想在访问页面(呈现组件)或更新页面后自动滚动到页面(组件)的底部。我尝试了参考文献和反应卷轴库,但没有成功。这是我的密码: 使用参考文献:Reactjs 如何在访问或更新页面后自动滚动到页面底部(react组件)?,reactjs,Reactjs,我想在访问页面(呈现组件)或更新页面后自动滚动到页面(组件)的底部。我尝试了参考文献和反应卷轴库,但没有成功。这是我的密码: 使用参考文献: class CommentsDrawer extends Component { componentDidMount() { this.scrollToBottom(); } componentDidUpdate() { this.scrollToBottom(); } scrollToBottom = () =&g
class CommentsDrawer extends Component {
componentDidMount() {
this.scrollToBottom();
}
componentDidUpdate() {
this.scrollToBottom();
}
scrollToBottom = () => {
this.messagesEnd.scrollIntoView({ behavior: "smooth" });
};
render() {
const { show, closeDrawer } = this.props;
let drawerClasses = styles.drawer;
if (show) {
drawerClasses = `${styles.drawer} ${styles.open}`;
}
return (
<div className={drawerClasses}>
<div>
<CommentsDrawerHeader closeDrawer={closeDrawer} numOfComments={mockedData.length} />
</div>
<CommentsList data={mockedData} />
{/* TODO: Reply functionality is just a mockup for this task scope. To be fully implemented in the integration task. */}
<CommentInput />
<div ref={(el) => { this.messagesEnd = el; }} />
</div>
);
}
}
class CommentsDrawer扩展组件{
componentDidMount(){
这个.scrollToBottom();
}
componentDidUpdate(){
这个.scrollToBottom();
}
scrollToBottom=()=>{
this.messagesEnd.scrollIntoView({behavior:“smooth”});
};
render(){
const{show,closeDrawer}=this.props;
let drawerClasses=styles.drawer;
如果(显示){
抽屉类=`${style.drawer}${style.open}`;
}
返回(
{/*TODO:回复功能只是此任务范围的一个模型。将在集成任务中完全实现。*/}
{this.messagesEnd=el;}}/>
);
}
}
解决方案是在组件的构造函数中创建引用。我已经在呈现注释的子组件中进行了实现
mport React, { Component, createRef } from "react";
import { array, func, boolean } from "prop-types";
import Comment from "../Comment/Comment";
import styles from "./CommentsList.scss";
class CommentsList extends Component {
static propTypes = {
// eslint-disable-next-line react/forbid-prop-types
data: array.isRequired,
sendReply: func,
deleteReply: func,
show: boolean
};
static defaultProps = {
sendReply: () => {},
deleteReply: () => {},
show: true
};
constructor(props) {
super(props);
this.bottomRef = createRef();
}
componentDidMount() {
this.scrollToBottom();
}
shouldComponentUpdate() {
if (!this.props.show) {
this.scrollToBottom();
}
}
scrollToBottom = () => {
this.bottomRef.current.scrollIntoView({ behavior: "smooth" });
};
render() {
const { data, sendReply, deleteReply } = this.props;
return (
<div className={styles.comments__list}>
{data.map(el => (
<Comment
// eslint-disable-next-line no-underscore-dangle
key={el._id}
// eslint-disable-next-line no-underscore-dangle
id={el._id}
by={el.by}
jobTitle={el.jobTitle}
timestamp={el.at}
comment={el.comment}
// eslint-disable-next-line no-undef
replyTo={!_.isUndefined(el.replyTo) && el.replyTo}
origin={el.origin}
sendReply={sendReply}
deleteReply={deleteReply}
/>
))}
<div ref={this.bottomRef} />
</div>
);
}
}
export default CommentsList;
mport React,{Component,createRef}来自“React”;
从“属性类型”导入{array,func,boolean};
从“./Comment/Comment”导入注释;
从“/CommentsList.scss”导入样式;
类CommentsList扩展了组件{
静态类型={
//eslint禁用下一行反应/禁止道具类型
数据:需要array.isRequired,
回复:func,,
答复:func,,
显示:布尔值
};
静态defaultProps={
sendReply:()=>{},
deleteReply:()=>{},
秀:真的
};
建造师(道具){
超级(道具);
this.bottomRef=createRef();
}
componentDidMount(){
这个.scrollToBottom();
}
shouldComponentUpdate(){
如果(!this.props.show){
这个.scrollToBottom();
}
}
scrollToBottom=()=>{
this.bottomRef.current.scrollIntoView({behavior:“smooth”});
};
render(){
const{data,sendReply,deleteReply}=this.props;
返回(
{data.map(el=>(
))}
);
}
}
导出默认注释列表;
这是否回答了您的问题?