简体   繁体   English

为什么我的 React 组件不渲染图像?

[英]Why isn't my React Component Rendering Images?

在此处输入图像描述 I am new to react, so I appreciate your patience.我是新来的反应,所以我感谢你的耐心等待。

I have the following folder structure:我有以下文件夹结构:

  • React-app>public>img (Has png images that won't load) React-app>public>img(有无法加载的 png 图像)
  • React-app>src>components (Has a component that needs to render the images) React-app>src>components(有一个需要渲染图片的组件)
  • React-app>data.js which looks like this (Note that these are snippets of the full code): React-app>data.js 看起来像这样(请注意,这些是完整代码的片段):
export const storeProducts = [
  {
    id: 1,
    title: "Google Pixel - Black",
    img: "../../public/img/product-1.png",
    price: 10,
    company: "GOOGLE",
    info:
      "Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
    inCart: false,
    count: 0,
    total: 0
  },

The component that is trying to render the image looks like this:尝试渲染图像的组件如下所示:

import React, { Component } from 'react';
import styled from 'styled-components';
import { Link } from 'react-router-dom';
import { ProductConsumer } from '../context';
import PropTypes from "prop-types";

class Product extends Component {

  render() {
    const { id, title, img, price, inCart } = this.props.product;
    console.log(img);
    return (
      <ProductWrapper className="col-9 mx-auto col-md-6 col-lg-3 my-3">
        <div className="card">
          <ProductConsumer>
            {(value) => (
              <div className="img-container p-5"
                onClick={() => value.handleDetail(id)}>
                <Link to="/details">
                  <img src={img} alt="product" className="card-img-top" />
                </Link>

I have tried moving the img folder into different paths.我试过将 img 文件夹移动到不同的路径。 I have tried changing the img src above to directly reference the image instead of the {img} object. The console log outputs the path to the image.我尝试更改上面的 img src 以直接引用图像而不是 {img} object。控制台日志输出图像的路径。

I am not sure if I should make the path of the image be relative to data.js, or product.js, but I have tried both.我不确定是否应该使图像的路径相对于 data.js 或 product.js,但我都尝试过。 I am sort of convinced that the path is not the problem since even directly referencing the image from the component didn't work.我有点确信路径不是问题,因为即使直接从组件引用图像也不起作用。

I have Googled and searched and haven't found anything that would help.我用谷歌搜索并搜索过,但没有找到任何有用的东西。

What am I missing?我错过了什么?

Edit: I guess the trick was to make the path relative to the public folder.编辑:我想诀窍是使路径相对于公用文件夹。 That's what did it for me.那就是为我做的。

在此处输入图像描述

Perhaps the path you're pointing to when calling store products array is wrong.也许您在调用商店产品数组时指向的路径是错误的。

What you should do is import the image you want to use in the storeproduct file你应该做的是导入你想在 storeproduct 文件中使用的图像

Something like this像这样的东西


Import img from  "../../public/img/product-1.png"

export const storeProducts = [
  {
    id: 1,
    title: "Google Pixel - Black",
    img,
    price: 10,
    company: "GOOGLE",
    info:
      "Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
    inCart: false,
    count: 0,
    total: 0
  },

And then use it inside your component like so然后像这样在你的组件中使用它


import styled from 'styled-components';
import { Link } from 'react-router-dom';
import { ProductConsumer } from '../context';
import PropTypes from "prop-types";

class Product extends Component {

  render() {
    const { id, title, img, price, inCart } = this.props.product;
    console.log(img);
    return (
      <ProductWrapper className="col-9 mx-auto col-md-6 col-lg-3 my-3">
        <div className="card">
          <ProductConsumer>
            {(value) => (
              <div className="img-container p-5"
                onClick={() => value.handleDetail(id)}>
                <Link to="/details">
                  <img src={img} alt="product" className="card-img-top" />
                </Link>

Cheers!干杯!

Try this:试试这个:

import image from "../../public/img/product-1.png"

export const storeProducts = [
  {
    id: 1,
    title: "Google Pixel - Black",
    img: image,
    price: 10,
    company: "GOOGLE",
    info:
      "Lorem ipsum dolor amet offal butcher quinoa sustainable gastropub, echo park actually green juice sriracha paleo. Brooklyn sriracha semiotics, DIY coloring book mixtape craft beer sartorial hella blue bottle. Tote bag wolf authentic try-hard put a bird on it mumblecore. Unicorn lumbersexual master cleanse blog hella VHS, vaporware sartorial church-key cardigan single-origin coffee lo-fi organic asymmetrical. Taxidermy semiotics celiac stumptown scenester normcore, ethical helvetica photo booth gentrify.",
    inCart: false,
    count: 0,
    total: 0,
  },
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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