{ { {

require an image in React

34 Views Asked by At

in my React code this return true:

console.log(`${person.image}` === "../images/profile/mehraboon.jpg");

and this works:

return (
    <section>{<img src={require("../images/profile/mehraboon.jpg")} alt="person" />}</section>
  );

but this does not work:

return (
    <section>{<img src={require(`${person.image}`)} alt="person" />}</section>
  );

why?

1

There are 1 best solutions below

0
jam j On

Here

console.log(`${person.image}` === "../images/profile/mehraboon.jpg");

person.image is not equivalent to image source & you never declare it with any variable

The standard code is your return result.

return (
    <section>{<img src={require("../images/profile/mehraboon.jpg")} alt="person" />}</section>
  );