I am using react perfectscrollbar https://www.npmjs.com/package/react-perfect-scrollbar
I am trying to implement the chat feature where on clicked on user, the chat message is requested via ajax and placed in the DOM. Here, I want the container to be scrolled at bottom.
I have implemented below code in react but it seems to be not working. Ant idea? Note: I have filtered the code and only pasted the related section. I have used the suggested method for scrollTop but it's not working.
class Complain extends Component {
constructor(props) {
super(props);
this.chatContainerRef = React.createRef();
}
viewComplain(e, id, name) {
axios.get('/complain/' + id, {
})
.then(res => {
const chatmsg = res.data.data;
// let data = { msgdata: chatmsg, name: name, complainid: id };
this.setState({ chatmsg: chatmsg });
this.setState({ complainname: name });
this.setState({ activeChat: id });
this.setState({ complainRegNo: chatmsg[0].regno });
console.log(this.chatContainerRef);
this.chatContainerRef.current.scrollTop = 200; // -> this is not working
})
.catch(function (error) {
});
}
render(
return {
<PerfectScrollbar option={{ suppressScrollX: false }} ref={this.chatContainerRef}>
{
this.state.chatmsg.map((post, i) => (
<div key={i} className={(post.msg_creater === "school") ? "row flex-nowrap message-row contact p-4" : "row flex-nowrap message-row user p-4"}>
<img className="avatar mr-4" src="../assets/images/avatars/profile.jpg" alt="" />
<div className="bubble">
<div className="message">{post.msg}</div>
<div className="time text-muted text-right mt-2">{post.created_at}</div>
</div>
</div>
))
}
</PerfectScrollbar>
})