简体   繁体   中英

Concatenate local path + dynamic images from Map - React.Js

i'm trying to do a CardList items from a local data.json file using React.Js.

To load the data i'm using a map function:

const ItemsList = data.map(item=>{

  return(
  <li><img key={item.id} src={"assets/images/sampleImages/" + item.image}/></li>
  )
})

Code - PasteBin

but i cannot get the image. No error, just a broken image icon appear.

I've try:

This solution And with URL works, but don't work with path.

Also this thread . And nothing works.

破碎的图像来说明。

Firstly import the image like this (You can amend it accordingly)

import imageBaseURL = "./assets/images/sampleImages/";

Then inside your ItemList make use of Template Literals like this :

const ItemsList = data.map( item => {
  return(
  <li><img key={item.id} alt="TitleOfImage" src={`${imageBaseURL}${item.image}`}/></li>
  )
})

First step:

Importing image and store it in one variable ( ExampleImg )

import ExampleImg from '../example.png';

Second step:

Inject the image as a src

<img src = { ExampleImg } />

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