简体   繁体   English

如何根据发送的道具渲染图像?

[英]How to render an image based on sent props?

I am new to react and currently trying to practice a little bit. 我是新手做出反应,目前正在努力练习一点。 I have made one component called 'Recensie', and I render a couple of them in my component. 我创建了一个名为'Recensie'的组件,我在我的组件中渲染了几个组件。

Within this review component I would like to display an image, based on the image prop I sent from the component. 在这个评论组件中,我想根据我从组件发送的图像道具显示图像。 However I cannot get it to work. 但是我无法让它发挥作用。 Currently this is my code: 目前这是我的代码:

import React from 'react'
function Recensie(props){
    return(
        <div className="recensie">
            {console.log(props.image)}
            <img src={`../imgs/${props.name}.png`}></img>
            <hr/>
            <h2>Lorem Ipsum</h2>
            <h2>Lorem Ipsum</h2>
        </div>
    )
}

export default Recensie

Where the main components code is like this: 主要组件代码是这样的:

import React from 'react'
import Recensie from './Recensie'

class Main extends React.Component{
    constructor(){
        super()
        this.state={

        }
    }

    render(){
        return(
            <div className="recensies">
                <Recensie name="one"/>
                <Recensie />
                <Recensie />
            </div>
        )
    }
}
export default Main

However, the image does not actually show up: https://i.imgur.com/dbvYTqE.png 但是,图像实际上并未显示: https//i.imgur.com/dbvYTqE.png

I have seen that I can import the exact image like this: 我已经看到我可以导入这样的确切图像:

import React from 'react'
import imgs from '../imgs/one.png'

function Recensie(props){
    return(
        <div className="recensie">
            {console.log(props.image)}
            <img src={imgs}></img>
            <hr/>
            <h2>Lorem Ipsum</h2>
            <h2>Lorem Ipsum</h2>
        </div>
    )
}

export default Recensie

However, this would mean I have to hardcode the image. 但是,这意味着我必须对图像进行硬编码。

Is there any way that I could do what I originally wanted to do, or is there a workaround I have to do? 有什么方法可以做我原本想做的事情,还是我有办法解决?

This is the simplest example I could come up with: 这是我能想到的最简单的例子:

class Recensie extends React.Component {
    state = {
        image: null,
    };

    componentDidMount() {
        import("./images/" + this.props.name).then((image) => {
            this.setState({ image: image.default });
        });
    }

    render() {
        return this.state.image ? (
            <img alt="image" src={this.state.image} />
        ) : null;
    }
}

Use it like so: 像这样使用它:

<Recensie name="test.png" />

It uses a dynamic import, which is asynchronous (async calls for data should be made in componentDidMount , see React docs for more info). 它使用动态导入,这是异步的(数据的异步调用应该在componentDidMount ,有关详细信息,请参阅React docs)。

Don't forget to adjust the path to your images folder correctly, based on where the Recensie component is located. 不要忘记根据Recensie组件的位置正确调整图像文件夹的路径。

I do it this way. 我是这样做的。 Create file imageLoader.js inside some utils folder 在某些utils文件夹中创建文件imageLoader.js

// imageLoader.js
// inside images object i name routes for my images from assets folder
// and export loadImage function

const images = {
  img1: require('../../assets/images/img1.jpg'),
  img2: require('../../assets/images/img2.png'),
  flagNotFound: require('../../assets/images/flag_not_found.png')
};

export function loadImage(imageName) {
  if (images.hasOwnProperty(imageName)) {
    return images[imageName];
  }
  return images['flagNotFound'];
}

Use it like 像它一样使用它

import {loadImage} from '../location-to-imageLoader'

<img alt="not found" src={loadImage('img1')}/>

In your case 在你的情况下

 <img alt="not found" src={loadImage(props.name)}/>

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

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