Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将事件侦听器附加到react中的componentDidUpdate_Javascript_Reactjs - Fatal编程技术网

Javascript 将事件侦听器附加到react中的componentDidUpdate

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(

我正在编写一个react web应用程序,它根据窗口帧的滚动位置动态呈现更多数据,以缩短初始加载时间。我的问题是,我需要知道附加数据何时完成更新,以便调用一个外部函数来跟踪新渲染的DOM元素的滚动位置

下面可以看到外部函数伪代码

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是您要从中分派事件的位置。

您能用您目前编写的代码补充您的伪代码吗?