[英]React - show loader while child nodes are loading
I have a modal window component that can take any type of HTML - inline, img tags, iframes etc. 我有一个模态窗口组件,可以采用任何类型的HTML-内联,img标签,iframe等。
Is it possible for the component to detect if the children have loaded? 组件是否可以检测子代是否已加载?
An example: 一个例子:
// app
<myComp>
<img src="some/remote/image.png" alt="" />
</myComp>
// myComp
render() {
const { children } = this.props;
// Pseudo code
const childrenLoading = children !== 'loading'; // <<< Possible to detect this??
return(
<div>
{childrenLoading ? '<p>loading</p>' : children}
</div>
);
}
I have looked into the React lifecycle methods, specifically componentDidUpdate, but of course myComp is not receiving new child data so it it doesn't trigger as an update. 我已经研究了React生命周期方法,特别是componentDidUpdate,但是myComp当然没有接收到新的子数据,因此它不会作为更新触发。
Perhaps it is not possible because the image / iframe is loading outside of my React app. 也许是不可能的,因为图像/ iframe正在我的React应用程序外部加载。 If I'm wrong please let me know.
如果我错了,请告诉我。
Thanks 谢谢
Use the onLoad event of the img tag to communicate with the children component. 使用img标签的onLoad事件与子组件进行通信。
<img onLoad={this.handleOnLoad} src="//the-src-of-the-image" />
https://reactjs.org/docs/events.html#image-events https://reactjs.org/docs/events.html#image-events
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.