0

I am trying to use images from a list as a source from a react component

import Button from 'react-bootstrap/Button';
import Card from 'react-bootstrap/Card';


const ProductComponent = ({ category, idx }) =>{
  const images = [
    './unisex-heavy-blend-hoodie-black-zoomed-in-2-638cce72d61d2.png',
    './unisex-staple-t-shirt-silver-front-638ca71569cc4.png',
    './unisex-staple-t-shirt-black-heather-zoomed-in-638ca1af35d94.png'
  ]
  return (
    <Card style={{ width: '25rem' }}>
      <Card.Img variant="top"  src={require(images[idx])} /> 
      <Card.Body>
        <Card.Title>{category}</Card.Title>
        <Button variant="primary">{images[idx]}</Button>
      </Card.Body>
    </Card>
  );
}

export default ProductComponent;

The src in doesn't work. The {images[idx]} in is for testing purposes, and it shows the link as it should. If i replace {images[idx]} in src with one of the link in the images list it works. I don't understand why. Could someone explain it? Thanks

I tried to put the link directly in src, and i printed out the {images[idx]} as a button to see its the right one

4
  • 1
    Where is the loop? Commented Dec 7, 2022 at 22:25
  • Possible duplicate: stackoverflow.com/questions/38374344/… Commented Dec 7, 2022 at 22:46
  • @Konrad I think the main goal is only one image is shown at a time images[idx], idx is passed as props. Commented Dec 7, 2022 at 22:47
  • 1
    @oser src={require(`${images[idx]}`)} should solve the issue as suggested on linked answer Commented Dec 7, 2022 at 22:49

0

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.