2

I have read many solutions on stack overflow and blogs but none could solve my problem. I have image urls from api. It is uncommon but it is an assignment to me

   productsdata= {
        id: 12,
        title: "Cat Tee Black T-Shirt",
        description: "4 MSL",
        availableSizes: ["S", "XS"],
        style: "Black with custom print",
        price: 10.9,
        installments: 9,
        currencyId: "USD",
        currencyFormat: "$",
        isFreeShipping: true,
        src_1: "../../assets/113_1.jpg",
        src_2: "../../assets/113_2.jpg"
      },

here I am displaying image as

{productsdata.map(product => {
        return (
          <>
            <img src={product.src_1} width="200" height="200" alt="tshirt" />
            {/* <img src={product.src_2} width="200" height="200" /> */}
          </>
        );
      })}

but it doesn't work as require() only works with static url. Could someone pls help me?

4
  • Please post your ReactJS code that generates the <img /> element. Commented Feb 7, 2020 at 10:04
  • 1
    <img src ={src1} /> Use Curly Braces if you have the api response in the jsx Commented Feb 7, 2020 at 10:04
  • 2
    How does img path from API can be a relative path? Seems strange. Commented Feb 7, 2020 at 10:04
  • 1
    Ask them to fix their API response as it makes no sense to send relative URLs in response. Commented Feb 7, 2020 at 10:05

2 Answers 2

3

I changed the image urls as

 productsdata= {

        src_1: "113_1.jpg",
        src_2: "113_2.jpg"
      },

and changed the 'src' attribute as

 <img src={require(`../../assets/${product.src_1}`)} width="200" height="200" alt="tshirt" />

It worked fine. Here also 'require' is using variable and dynamic url. Who on the earth would think of writing this way. I think it's a bug in javascript or I don't have enough understanding:p

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

Comments

-1

you should use like this-

import React, {useState} from 'react';
    import Products from './old';
 const New = (props) =>{
    
    return(
        <div>
       {Products.map((Product, i)=>(
         Object.values(Product).map((prod, i) => (
            <div key={i}>
                <p>id: {prod.id}</p>
                <p>category: {prod.category}</p>
                <p>filename: {prod.filename}</p>
                <p>name: {prod.name}</p>
                <p>price: {prod.price}</p>
            </div>
          )) 
       )

       )}
        </div>
    );
}
export default New;

4 Comments

where old.js file will look like this==========> const Products = [ { "867850": { "id": "867850", "category": "Clothes", "filename": "purse", "name": "Purse", "price": 45.48 }}]; export default Products;
I did not get it. How could this solve my problem with rendering image
Use var Products = require('./old') ; and change Products.map to => Products.default.map
Thanks for your concern but I did not understand what you were saying

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.