【问题标题】:React: Trying to render images from public folder dynamically in iterated componentReact:尝试在迭代组件中动态渲染来自公共文件夹的图像
【发布时间】:2021-03-23 01:20:09
【问题描述】:

这是我的第一个问题。我已经搜索了几天,无法弄清楚我做错了什么。我是 React 和整个开发的新手,所以这很可能是我最大的障碍,但是,据我所知,这应该可行。

我正在尝试在迭代组件中动态渲染图像。图像本地托管在公用文件夹中。在我解决这个问题时,我正在使用 JSON 服务器来迭代我的数据。当我在组件中使用完整路径时,一切都很好:

<figure className="stone__card">
    <img className="stone__img"
        src={`${process.env.PUBLIC_URL}/assets/img/stone/BiancoSivec1.jpg`}
        alt="BiancoSivec"
    />
    <h3 className="stone__card-head">Bianco Sivec</h3>
    <p className="stone__card-type">Marbre</p>
</figure>

但是,由于我需要迭代和渲染许多项目,因此我动态尝试相同的方法,只是图像损坏了:

<figure className="stone__card">
  <img className="stone__img"
    src={stone.image}
    alt={stone.name}
   />
  <h3 className="stone__card-head">{stone.name}</h3>
  <p className="stone__card-type">{stone.type}</p>
</figure>

当我 console.log stone.image 时,我会收到相应的数据:

{`${process.env.PUBLIC_URL}/assets/img/stone/BiancoSivec1.jpg`}

我曾尝试在我的 db.json 文件中删除/添加大括号,但无济于事。我错过了什么,或者只是不允许这样做?我去过 React docs Using Public Folder,也没有找到任何东西。有人可以指出我正确的方向吗?

这是我正在使用的 db.json 文件:

{
    "stones": [
        {
            "id": 1,
            "important": true,
            "name": "Bianco Sivec",
            "type": "Marbre",
            "color": "Blanc",
            "origin": "Macédoine",
            "finish": "Poli",
            "thickness": "3cm, 2cm",
            "image": "{`${process.env.PUBLIC_URL}/assets/img/stone/BiancoSivec1.jpg`}"
        },
        {
            "id": 2,
            "important": true,
            "name": "Imperial White",
            "type": "Marbre",
            "color": "Blanc",
            "origin": "Macédoine",
            "finish": "Poli",
            "thickness": "3cm, 2cm",
            "image": "{`${process.env.PUBLIC_URL}/assets/img/stone/ImperialWhite1.jpg`}"
        },
        {
            "id": 3,
            "important": true,
            "name": "Fantasy White",
            "type": "Marbre",
            "color": "Blanc",
            "origin": "Macédoine",
            "finish": "Poli",
            "thickness": "3cm, 2cm",
            "image": "{`${process.env.PUBLIC_URL}/assets/img/stone/FantasyWhite1.jpg`}"
        },
        {
            "id": 4,
            "important": true,
            "name": "Infinito",
            "type": "Marbre",
            "color": "Blanc",
            "origin": "Macédoine",
            "finish": "Poli",
            "thickness": "3cm, 2cm",
            "image": "{`${process.env.PUBLIC_URL}/assets/img/stone/Infinito1.jpg`}"
        }
    ]
}

这是实际日志的图像:

Image of log

【问题讨论】:

  • 当你 console.log stone.image 你应该收到一个包含公共 URL 的字符串。如果您收到{${process.env.PUBLIC_URL}/assets/img/stone/BiancoSivec1.jpg},那么您可能忘记使用模板文字。您如何存储和/或加载图像的 URL?
  • 嗨,你能提供一个你的 JSON 文件的例子吗?
  • 您好 Zsolt,缺少的模板文字是我发布问题的一个错误。它现在固定了。除非你的意思是我的日志应该用 "" 包裹起来,否则它不是。
  • 嗨 lissettdm,我发布了 db.json

标签: reactjs image dynamic render public


【解决方案1】:

JSON 格式不支持模板文字,也没有环境变量。

因此,您需要删除模板文字并将您的json 文件转换为以下形式,以便获得相对路径:

{
    "stones": [
        {
            "id": 1,
            "important": true,
            "name": "Bianco Sivec",
            "type": "Marbre",
            "color": "Blanc",
            "origin": "Macédoine",
            "finish": "Poli",
            "thickness": "3cm, 2cm",
            "image": "assets/img/stone/BiancoSivec1.jpg"
        },
        {
            "id": 2,
            "important": true,
            "name": "Imperial White",
            "type": "Marbre",
            "color": "Blanc",
            "origin": "Macédoine",
            "finish": "Poli",
            "thickness": "3cm, 2cm",
            "image": "assets/img/stone/ImperialWhite1.jpg"
        },
        {
            "id": 3,
            "important": true,
            "name": "Fantasy White",
            "type": "Marbre",
            "color": "Blanc",
            "origin": "Macédoine",
            "finish": "Poli",
            "thickness": "3cm, 2cm",
            "image": "assets/img/stone/FantasyWhite1.jpg"
        },
        {
            "id": 4,
            "important": true,
            "name": "Infinito",
            "type": "Marbre",
            "color": "Blanc",
            "origin": "Macédoine",
            "finish": "Poli",
            "thickness": "3cm, 2cm",
            "image": "assets/img/stone/Infinito1.jpg"
        }
    ]
}

并在组件中,使用模板文字,将相对链接转换为绝对路径。

<figure className="stone__card">
  <img className="stone__img"
    src={`${process.env.PUBLIC_URL}/${stone.image}`}
    alt={stone.name}
   />
  <h3 className="stone__card-head">{stone.name}</h3>
  <p className="stone__card-type">{stone.type}</p>
</figure>

【讨论】:

  • 感谢您抽出宝贵时间对此进行解释。我也明白为什么它现在也不起作用了。
  • @JonDennis 你达到预期的结果了吗?
猜你喜欢
  • 2019-10-06
  • 2014-12-18
  • 2015-02-10
  • 1970-01-01
  • 2022-01-25
  • 2018-07-21
  • 2020-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多