简体   繁体   中英

Can't load local img in react using require

I would like to render local images inside a map loop.

I have seen in many posts that I should use require

const urlBase = './../../assets/';
const CompList = (props) => {
    return props.skills.map((elm, index) => {
        console.log(`${urlBase}${elm.imageName}`)
        return <Chip
            key={`comp-${index}`}
            avatar={<Avatar alt="Angular" src={require(`${urlBase}${elm.imageName}`)} />}
            label={elm.techno}
        />
    })
}

But I'm getting this error

Cannot find module './../../assets/Angular.png'

The app was generated using create-react-app

You can do this way -

const imageToShow = require('../path-to-the-file.extension');

<Chip
  key={`comp-${index}`}
  avatar={<Avatar alt="Angular" src={imageToShow} />}
  label={elm.techno}
  />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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