【发布时间】:2021-01-20 19:27:08
【问题描述】:
我想动态渲染图像,但没有显示任何内容。这是我从Import image dynamically in React component 使用的起始代码。
import React, { Component, Fragment } from 'react';
class Test extends Component{
constructor(props){
super(props);
this.state = {
image: "",
}
this.loadImage = this.loadImage.bind(this);
}
componentDidMount(){
this.loadImage("Test")
}
loadImage = imageName => {
import(`../assets/${imageName}.png`).then(image => {
this.setState({
image
});
});
};
render() {
const { image } = this.state;
return (
<Fragment>
hello
{image && <img src={image} alt="" />}
</Fragment>
);
}
}
export default Test;
你好渲染,但图像在哪里看不到。有什么想法
【问题讨论】:
标签: reactjs