Recently I wrote in my blog that in the project list, I found that there were many pictures here, and the loading would be slow at the beginning, so I wanted to use a EffectPrinciple Analysis
Picture of the event There are many events for images, for example, Component Codeimport { ImgHTMLAttributes } from "react"; /** * Image placeholder component properties*/ export interface IImagProps<T> extends ImgHTMLAttributes<T> { /** * Loading images */ loadingImg?: string, /** * Failed to load the image */ errorImg?: string, /** * The address where the picture is normally displayed*/ src: string, } import React, { useState } from 'react' // The following two are to import the default imagesimport loadImg from './../../../assets/imgs/loading/load.gif'; import errorImg from './../../../assets/imgs/loading/error.png' export default function Img(props: IImagProps<any>) { // Image address const [src, setSrc] = useState(props.loadingImg as string) // Is it the first time to load? If you don't use this, it will be loaded twice const [isFlag, setIsFlag] = useState(false) /** * Image loading completed*/ const handleOnLoad = () => { // Determine if it is the first time to load if (isFlag) return; // Create an img tag const imgDom = new Image(); imgDom.src = props.src; // Image loading is complete, use the normal image imgDom.onload = function () { setIsFlag(true) setSrc(props.src) } // Image loading failed, use image placeholder imgDom.onerror = function () { setIsFlag(true) setSrc(props.errorImg as string) } } return ( <> <img src={src} onLoad={handleOnLoad} style={{ height: 'inherit', }} ></img> </> ) } // Set the default style for loading images and failed images Img.defaultProps = { loadingImg: loadImg, errorImg: errorImg } PS: Let's take a look at the loading effect before the img image is loaded in React
// Suppose I want to load these three web images var imglist = ['http://example.com/demo1.png','http://example.com/demo2.png','http://example.com/demo3.png'] // images is used to store the loaded images var images = [] imglist.forEach(el=>{ var image = new Image() image.src = el image.onload = function(){ // Indicates that the image has been loaded. // Add the loaded image to images images.push(image) } }) //Judge when the component is rendered if(images.length === 3){ // This means that all three web page images have been loaded and can be rendered. // Render loaded images. }else{ // This means that the web page images have not been fully loaded yet, so the loading animation effect will continue. // loading animation effect} Specific implementation examples import React from 'react' import { Carousel, Spin } from 'antd' // use antd // Create the Home component class Home extends React.Component{ constructor(props){ super(props) this.state = { imglist: [ { id: '01', src: 'http://example.com/demo1.png', alt: 'demo1' }, { id: '02', src: 'http://example.com/demo2.png', alt: 'demo2' }, { id: '03', src: 'http://example.com/demo3.png', alt: 'demo3' } ], images: [] } } UNSAFE_componentWillMount(){ // Perform operations before rendering var { imglist } = this.state var images = [] imglist.forEach(el=>{ var image = new Image() image.src = el.src image.onload = ()=>{ images.push(image) this.setState({ images }) } }) } render(){ var { imglist, images } = this.state if(images.length === 3){ // This means that all three images have been loaded and can be rendered. return ( <div className='common-body'> <Carousel autoplay> {imglist.map(el=>( <img src={el.src} key={el.id} alt={el.alt} /> ))} </Carousel> </div> ) }else{ // The image has not been fully loaded yet, so the loading animation effect should be displayed at this time return ( <div className='common-loading'> <Spin tip='Loading...' size='large'></Spin> </div> ) } } } export default Home This method is still more useful The above is the detailed content of the principle analysis of the three stages of react's implementation of image loading, loading completed, and loading failed. For more information about react's image loading completion, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Perfect Solution for No rc.local File in Linux
>>: The easiest way to install MySQL 5.7.20 using yum in CentOS 7
Writing a Dockerfile Taking the directory automat...
CentOS 8 is officially released! CentOS fully com...
Table of contents Message Board Required librarie...
Table of contents Project Introduction: Project D...
In Vue, we generally have front-end and back-end ...
This article example shares the specific code of ...
Core code -- Below I will demonstrate the impleme...
This article example shares the specific code of ...
1. Linux installation (root user operation) 1. In...
The virtual machine is installed on the host mach...
This article shares the specific process of js ob...
Table of contents Problems with resource manageme...
First, install PHP5 very simple yum install php T...
Comprehensive understanding of html.css overflow ...
Be careful when listening for events that are tri...