I want to scroll automatically to the bottom of a page (component) after visiting the page (render the component) or updating it. I have tried with refs and react-scroll library but no success. Here is my code:
Using refs:
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>
);
}
}