admin管理员组文章数量:1291031
I'm trying to scroll to the end of my FlatList
when the state of messages
changes. Currently I'm using this.refs.flatList.scrollToEnd();
after waiting a bit, which works OK. What I want to do is add an event listener to the messages
state so that when the list is changed in any way it will scroll to the end of the list. I tried
ponentDidMount(){
this.state.messages.addEventListener('change', this._handleNewMessage);
}
but it doesn't work. Does anyone know a way I can achieve this?
I'm trying to scroll to the end of my FlatList
when the state of messages
changes. Currently I'm using this.refs.flatList.scrollToEnd();
after waiting a bit, which works OK. What I want to do is add an event listener to the messages
state so that when the list is changed in any way it will scroll to the end of the list. I tried
ponentDidMount(){
this.state.messages.addEventListener('change', this._handleNewMessage);
}
but it doesn't work. Does anyone know a way I can achieve this?
Share Improve this question edited Oct 22, 2018 at 17:09 Sateesh Yemireddi 4,4091 gold badge22 silver badges39 bronze badges asked Oct 22, 2018 at 14:25 jacob blankenshipjacob blankenship 1711 gold badge4 silver badges19 bronze badges 1- "when the list is changed in any way" what do you mean by any way? – Sagiv b.g Commented Oct 22, 2018 at 14:27
1 Answer
Reset to default 7This would be what the lifecycle method ponentDidUpdate
is for:
ponentDidUpdate()
is invoked immediately after updating occurs. This method is not called for the initial render.Use this as an opportunity to operate on the DOM when the ponent has been updated. ...
Naturally, you have to ensure that this.state.messages
is only changed via setState
—but that was already true, using ponentDidUpdate
doesn't change that.
ponentDidUpdate() {
this.refs.flatList.scrollToEnd();
}
本文标签: javascripthow to add an event listener to a state in react nativeStack Overflow
版权声明:本文标题:javascript - how to add an event listener to a state in react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741514695a2382812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论