0

I need assistance of displaying the images without import or any external component. I have tried all the below methods but nothing worked out. Any assistance on this matter is much appreciated.

When i use require(), I get the error a Cannot find module '../assets/images/products

import React from "react";
    
export default function ProductGallery() {
  const products = [
    {
      ProductImage: "../assets/images/products/image1.jpg",
    },
    {
      ProductImage: "../assets/images/products/image2.jpg",
    },
  ];

  return (
    <div className="row">
      {products.map((item, index) => (
        <div className="galleryBox" key={index}>
          <img src={require(item.ProductImage)}/>
          <img src={require(""+item.ProductImage)}/>
          <img src={require(`${item.ProductImage}`)}/>
          // all the three methods didnt work
          <img src={item.ProductImage}/>
          // this doesnt show image at all. When I Inspect, it shows the path correct. But it should come from ../static, however it showing actual path
        </div>
      ))}
    <div>
  );
}

3 Answers 3

1

Ciao, unfortunately require function does not accept dynamic resources. The problem is related to the fact require function will be loaded before runtime and, if it doesn't find a resource at that time, it doesn't work.

A possible workaround is made an object with all the resources you need like:

export default resources = {
   image1: require('../assets/images/products/image1.jpg'),
   image2: require('../assets/images/products/image2.jpg')
}

Then import this object in your component and use it like this:

<img src={resources.image1}/>
<img src={resources.image2}/>             

It's the only way I found to have dynamic resources with require function.

Sign up to request clarification or add additional context in comments.

7 Comments

export default resources = { image1: require('../assets/images/products/image1.jpg'), image2: require('../assets/images/products/image2.jpg') } But that's coming it will be coming from the backend. I will assign this to the data coming from the backend later.
in that case, i might not be able to use require
Sorry I don't understand. What is coming from backed? resources it's just an object that you could initialized in a separated file of your project and import in component in which you need to show image1 or image2.
i will get this image resource as a json data from my api. so the json data will look something like the products array.
Ok understood. The problem becames harder. But let me understand better: you have your images in '../assets/images/products/image1.jpg' that si a folder in your project and then you asking to the backend to provides you the path to these resources correct?
|
0

Place the image in the Public folder, so that, the reference path can pull the image.

Comments

0
<img loading='lazy' src={require('../../assets/images/logo.svg').default} alt="logo" />

Comments

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.