1

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.

Broken images to illustrate.

2
  • Have you tried with "require"? It should be like this: src={require('./assets/images/sampleImages/${item.image}')} Commented Feb 15, 2018 at 11:20
  • Can you please post your JSON data? Commented Feb 16, 2018 at 7:02

2 Answers 2

2

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>
  )
})
Sign up to request clarification or add additional context in comments.

Comments

0

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 } />

1 Comment

i cannot store it in one variable because the path it's static and the name.jpg change according the image. maybe 2 variables ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.