[Web 前端] React Js img 圖片顯示默認 占位符


cp from : https://blog.csdn.net/wyk304443164/article/details/77093339

 

沒有考慮到兼容性,因為我們暫時只適配了webkit。 
也沒有考慮到懶加載,因為項目比較緊有需要加的朋友看react-lazyload,也比較簡單,現成的輪子

/**
 * Created by wuyakun on 2017/8/11.
 * 會顯示默認圖片的image
 */
import React from 'react';

class DefaultImage extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            src: this.props.src ? this.props.src : '',
        }
    }

    handleImageLoaded() {
        //加載完畢
    }

    handleImageErrored() {
        //加載失敗
        this.setState({
            src: require('../../images/default.jpg')
        });
    }

    render() {
        let props = this.props;
        let {src} = this.state;
        return (
            <img
                {...props}
                src={src}
                onLoad={this.handleImageLoaded.bind(this)}
                onError={this.handleImageErrored.bind(this)}
            />
        );
    }
}

export default DefaultImage;

 

 
         
         
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM