Javascript 将事件侦听器附加到react中的componentDidUpdate
我正在编写一个react web应用程序,它根据窗口帧的滚动位置动态呈现更多数据,以缩短初始加载时间。我的问题是,我需要知道附加数据何时完成更新,以便调用一个外部函数来跟踪新渲染的DOM元素的滚动位置 下面可以看到外部函数伪代码Javascript 将事件侦听器附加到react中的componentDidUpdate,javascript,reactjs,Javascript,Reactjs,我正在编写一个react web应用程序,它根据窗口帧的滚动位置动态呈现更多数据,以缩短初始加载时间。我的问题是,我需要知道附加数据何时完成更新,以便调用一个外部函数来跟踪新渲染的DOM元素的滚动位置 下面可以看到外部函数伪代码 this.renderAdditionalData(child); //Once the child has actually displayed all of the additional data(DOM elements updated) new Promise(
this.renderAdditionalData(child);
//Once the child has actually displayed all of the additional data(DOM elements updated)
new Promise((res, rej) =>{
child.mapNewLines(res);//this method calls res after all the processing has finished
}).then(() =>{this.doSomethingWithNewData(foo);});
在屏幕上更新数据后,我该如何启动此承诺?因为mapNewLines使用新渲染数据的DOM属性,而不是使用承诺,您将在componentDidUpdate(react中的方法)中发出事件因为componentDidUpdate是在呈现和DOM属性更新之后运行的,所以您可以知道在页面上呈现新项目的时间。然后,当渲染更多数据以触发mapNewLines时,您将附加一个eventListener来侦听。然后将dosomethingwithNewData作为回调附加到mapNewLines函数
let callback = (this.doSomethingWithNewData.bind(this, timestamp, element));
window.addEventListener('mapped', () => {element.mapNewLines(callback);});
this.renderAdditionalData(child);
where window是您要从中分派事件的位置。您能用您目前编写的代码补充您的伪代码吗?