React.js 不会加载本地文件,将背景图像样式属性作为道具传递

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

我试图在呈现我制作的组件时将 style 属性作为道具传递。当我将本地图像作为 background: 传递时,它不会加载,但如果它是图像地址,它会按原样加载。 Here is the folder structure.

这是我传递道具的方式:

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

非常感谢您的帮助!

尝试

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

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