简体   繁体   English

页面加载后反应调用功能

[英]react call function after page has loaded

I want to display images onDrop in one of my components. 我想在我的一个组件中显示onDrop图像。 To make the drop more seamless I want to preload the images like this: 为了使drop更加无缝,我想像这样预加载图像:

componentDidMount(){
   this.props.photos.forEach(picture => {
      const img = new Image();
      img.src = picture.url;
    });
}

However I don't want to slow down my initial load time by preloading all these images, so I want to only load them after everything else has loaded. 但是我不想通过预加载所有这些图像来减慢我的初始加载时间,因此我只想在加载其他所有图像后加载它们。

I tried putting the loading function in a sepperate component and then loaded it in like this: 我尝试将加载函数放在一个sepperate组件中,然后像这样加载:

const LazyImgLoader = import("./ImgLoader ");
const ImgLoader = React.lazy(() => LazyImgLoader);

but that didn't work and seems kind of hacky aswell. 但这不起作用,似乎也有点hacky。 How can I load my images in after my page has loaded? 如何在我的页面加载后加载我的图像?

It looks as though you need to load the images asynchronously. 看起来好像需要异步加载图像。

Happily this is built into the Image class provided with React Native (of course I am making the assumption that this is what you are using). 令人高兴的是,它内置于React Native提供的Image类中(当然我假设这是你正在使用的)。

See the documentation , particularly the onLoad, onLoadStart and onLoadEnd callbacks. 请参阅文档 ,特别是onLoad,onLoadStart和onLoadEnd回调。 These should enable you to use placeholders for your images until they are loaded and only present them once the loading is complete. 这些应该允许您在图像加载之前使用占位符,并且只在加载完成后才显示它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM