简体   繁体   English

添加图像以响应动态链接

[英]adding images to in react with dynamic links

I have created react app using create-react-app , there is a component which need to be reused multiple times, this component contain <img> tag, image is different for each instance of the component.我使用 create-react-app 创建了反应应用程序,有一个组件需要多次重用,该组件包含<img>标签,组件的每个实例的图像都不同。 I am passing image path as imagePath prop from parent to child component and then that child need to load image from that path.我将图像路径作为imagePath道具从父组件传递到子组件,然后该子组件需要从该路径加载图像。 i can not use我不能用

import image from imagePath

because i don't know the path until component is build, and import statement don't work within component body.因为在构建组件之前我不知道路径,并且 import 语句在组件主体中不起作用。

I tried doing <img src={imagePath}> that also don't work, can you point me to right direction?我试过做<img src={imagePath}>也不起作用,你能指出我正确的方向吗?

adding code for further clarification添加代码以进一步说明

first approach it doesnt work.一种方法它不起作用。 content is object passed by parent and content.image have value of ./images/keeper.PNG content 是由父对象传递的对象,而 content.image 的值为 ./images/keeper.PNG

    import React from 'react'; 
import image from './images/keeper.PNG'

    export default function Project(props)

    {

        return <div className='projectContainer'  >
            <div className='projectImage'>
                <img src={props.content.image}
            alt ='' />
            </div>
            <div className='.projectDescription'>
                <h4>{props.content.name}</h4>
                <p>{props.content.intro}</p>
                <h5>Technologies and problems</h5>
                <p>{props.content.tech}
                </p>
            </div>
        </div> }

second methond <img src={image} alt ='' /> it works fine and show image but as stated above i don't know image path before the component is created第二种方法<img src={image} alt ='' />它工作正常并显示图像但如上所述我不知道组件创建之前的图像路径

Sample样本

Parent component父组件

let image = 'https://www.belightsoft.com/products/imagetricks/img/intro-video-poster@2x.jpg'

function Parent(){
 return <Child image={image}/>
}

Child component子组件

function Child(props){
 return <img src={props.image} alt="icon-image" />
}

or或者

directly if you import in component如果您在组件中导入,则直接

import imagePath from './image.jpg';从 './image.jpg' 导入 imagePath;

   function Child(props){
     return <img src={imagePath} alt="icon-image" />
    }

Try using it this way, where you can pass the imagePath via props尝试以这种方式使用它,您可以在其中通过 props 传递 imagePath

<img src={require(`${props.imagePath}`)} />

This alone will do the job.仅此一项就可以完成工作。

When using Webpack you need to require images in order for Webpack to process them, which would explain why external images load while internal do not, so instead of img src= {"/imagePath"} you need to use img src= {require('/imagePath')} replacing imagePath with the correct image name for each of them.使用 Webpack 时,您需要要求图像以便 Webpack 处理它们,这可以解释为什么外部图像加载而内部图像不加载,因此您需要使用 img src= {require( '/imagePath')} 将 imagePath 替换为每个图像的正确图像名称。 That way Webpack is able to process and replace the source img.这样 Webpack 就能够处理和替换源 img。

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

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