Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React.js won't load a local file, passin background image style attribute as a prop

I’m trying to pass style attribute as a prop when rendering a component I’ve made. When I pass a local image as background: it won’t load, but if it is an image address it loads as it should. Here is the folder structure.

Here is the how I’m passing the prop:

      <Key
        style={{
          background: require("./keyImages/0.jpeg"),
          backgroundSize: "70px",
        }}
      />

Thanks a lot in advance for help!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try

import imgUrl from "./keyImages/0.jpeg";

      <Key
        style={{
          backgroundImage: 'url(' + imgUrl + ')',
          backgroundSize: "70px",
        }}
      />

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading