react.js Replace img src onerror

后端 未结 22 2962
北恋
北恋 2020-11-29 00:20

I have a react component that is the detail view from a list.

I am trying to replace the image with a default image if the image does not exist and there is a 404 er

相关标签:
22条回答
  • 2020-11-29 01:08

    This works best for me

    <img src={record.picture} onError={(e)=>{e.target.onerror = null; e.target.src="image_path_here"}}/>
    
    0 讨论(0)
  • 2020-11-29 01:10

    this works for me .

    {<img className="images"
        src={`/images/${student.src ? student.src : "noimage.png" }`} alt=  
    {student.firstname} />} 
    

    student is the name of my array and noimage the image, when there is no image is display.

    0 讨论(0)
  • 2020-11-29 01:11

    You can use uncontrolled component:

    <img src={this.state.img} ref={img => this.img = img} onError={
        () => this.img.src = 'img/default.img'
    }>
    
    0 讨论(0)
  • 2020-11-29 01:12

    Even though this is an old question if you are looking of a clean solution you can use react-image-fallback library.

    <ReactImageFallback
                        src="my-image.png"
                        fallbackImage="my-backup.png"
                        initialImage="loader.gif"
                        alt="cool image should be here"
                        className="my-image" />
    

    react-image-fallback

    0 讨论(0)
  • 2020-11-29 01:13

    Try this custom Image component:

    import React, { useRef } from 'react';
    import PropTypes from 'prop-types';
    
    import defaultErrorImage from 'assets/images/default-placeholder-image.png';
    
    const Image = ({ src, alt, className, onErrorImage }) => {
      const imageEl = useRef(null);
      return (
        <img
          src={src}
          alt={alt}
          className={className}
          onError={() => {
            imageEl.current.src = onErrorImage;
          }}
          ref={imageEl}
        />
      );
    };
    
    Image.defaultProps = {
      onErrorImage: defaultErrorImage,
    };
    
    Image.propTypes = {
      src: PropTypes.string.isRequired,
      alt: PropTypes.string.isRequired,
      className: PropTypes.string.isRequired,
      onErrorImage: PropTypes.string,
    };
    
    export default Image;
    
    0 讨论(0)
  • 2020-11-29 01:14

    You need just define onError handler than change the state which will trigger component render method and eventually component will re-render with placeholder.

    Please, don't use jQuery and React together!

    import React from 'react';
    import {Link} from 'react-router';
    import ContactStore from '../stores/ContactStore'
    import ContactActions from '../actions/ContactActions';
    
    class Contact extends React.Component {
      constructor(props) {
        super(props);
        this.state = ContactStore.getState();
        this.onChange = this.onChange.bind(this); 
     }
    
    componentDidMount() {
      ContactStore.listen(this.onChange);
      ContactActions.getContact(this.props.params.id);
    }
    
    componentWillUnmount() {
      ContactStore.unlisten(this.onChange);
    }
    
    componentDidUpdate(prevProps) {
      if (prevProps.params.id !== this.props.params.id) {
        ContactActions.getContact(this.props.params.id);
      }
    }
    
    onChange(state) {
      this.setState(state);
    }
    
    onError() {
      this.setState({
        imageUrl: "img/default.png"
      })
    }
    
    render() {
      return (
        <div className='container'>
          <div className='list-group'>
            <div className='list-group-item animated fadeIn'>
              <h4>{this.state.contact.displayname}</h4>
              <img onError={this.onError.bind(this)} src={this.state.imageUrl} />
            </div>
          </div>
        </div>
      );
    }
    
    export default Contact;
    
    0 讨论(0)
提交回复
热议问题