简体   繁体   中英

ReactJS jsx load img URI from a string (doesn't work)

Trying to load an image URI from props and not having any luck. I can of course load the image with:

import logoURI from '../assets/image.png';
<...>
<img src={logoURI} />

but I need to get away from the static URI and load it from props.

This works:

<div className="logo">
  <img
    src={require("../assets/image.png")}
  />
</div>

But this does not ( Cannot find module '../assets/image.png' ):

var logoURI = "../assets/image.png";
return (
  <div className="logo">
    <img
      src={require(logoURI)}
    />
  </div>
)

A string is a string? How do I make this work? In my terminal I see:

Critical dependency: the request of a dependency is an expression

Try prepending your logoURI with ./ :

var logoURI = "./../assets/image.png";

EDIT

If you need to require anything like that, you need to install request :

npm install request -g 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM