1

I'm trying to import images into a list dynamically. some images are missing, so I did a try catch to check if the image exists, otherwise I have one by default.

    const setSource = (src) =>{
        const imgTMP = dirImg + src + ".jpg";
        try{
            require(imgTMP)
            // console.log('ok', src);
            return imgTMP;
        }
        catch(err){
             // console.log('none', src);
            return "../../assets/artwork/h.jpg";
        }
    }
                    <Artwork.Thumbnail>
                        <img 
                            src= {require(setSource(art.id).toString())}
                            alt='art'/>
                    </Artwork.Thumbnail>

the concern is that the try/catch refuses to work properly.

moreover the character string returned (even valid) returns an

Uncaught Error: Cannot find module 

tree project

Thanks for your help

fixed by this class:

import React from "react";

//Lazy loading images 
//PROPS: src, alt, className, onError, unloadedSrc
//?props: onError : directory of the image if not found
//?porps: className : className of the image
//?props: alt: alt of the image
//?props: src : src of the image
//?props: unloadedSrc : src of the image not loaded
export class LazyImage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loaded: false,
      error: false
    };
  }

  componentDidMount() {
    const img = new Image();
    img.onload = () => {
      this.setState({
        loaded: true
      });
    };
    img.onerror = () => {
      this.setState({
        error: true
      });
    };
    img.src = this.props.src;
  }

  render() {
    if (this.state.error) {
      return <img
        className={this.props.className}
        style={this.props.style}
        src={this.props.unloadedSrc}
        alt={this.props.alt} />
    } else if (!this.state.loaded) {
      return <img
        className={this.props.className}
        style={this.props.style}
        src={this.props.unloadedSrc}
        alt={this.props.alt} />
    }
    return <img
      className={this.props.className}
      style={this.props.style}
      src={this.props.src}
      alt={this.props.alt} />
  }
}
  • does this help https://stackoverflow.com/questions/37644265/correct-path-for-img-on-react-js/49785719#49785719 – Prince Sodhi Aug 03 '22 at 20:17
  • that's what i'm doing if i understood correctly, in second code block – jonathan gleyze Aug 03 '22 at 20:19
  • Hey, look at this is this what you are doing? I think what you are doing should work once complied but before might be buggy, trying doing a build and testing if it works. Another way could be to do a errorBoundary that displays that fall back image instead.https://codesandbox.io/s/image-failed-module-xx0gjc?file=/src/App.js – Colin Hale Aug 03 '22 at 20:50
  • thanks I try with the functions that have in the codepen. – jonathan gleyze Aug 03 '22 at 20:56
  • unfortunately the problem remains the same :/ – jonathan gleyze Aug 03 '22 at 21:05

0 Answers0