尝试显示图像时出错
Getting an error when trying to display images
我正在尝试使用 React js 制作一个照片库应用程序。我将图像的 url 存储在 firebase 实时数据库中,但是当我尝试使用 img 标签显示这些图像时出现错误。
Error: img is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.
The above error occurred in the <img> component:
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
这就是我从 firebase
获取 url 的方式
componentDidMount(){
axios.get('/Data.json')
.then(response => {
const fetchedData = [];
for(let key in response.data){
fetchedData.push({
...response.data[key],
id: key
});
}
this.setState({loading: false, data: fetchedData});
})
.catch(error => {
console.error(error)
this.setState({loading:false});
});
}
这就是我尝试显示图像的方式
this.state.data.reverse().map((res) => (
<div className="card1">
<Card
key={res.id}
style={{backgroundColor:"#343a40", textAlign:"left" ,
margin:"20px" ,color: "white",
left:"370px", borderRadius:"10px",
overflow:"hidden", width:"600px",
height:"200px", boxShadow:"0 10px 18px 0 rgba(0,0,0,0.2)"}}
>
<Card.Body
className="container">
<h4>
ANONYMOUS
</h4>
<Card.Text>
{res.Comment}
</Card.Text>
<Card.Img>
<img src={res.ImageUrl} width = "400px" height="150px" />
</Card.Img>
<Button className="btn btn-danger"
style={{float:"right", width:"40px"}}
onClick={() => this.DeleteCommentHandler(res.id)}>
<FontAwesomeIcon icon={faTrash}/>
</Button>
</Card.Body>
<Card.Footer>
{res.Date}
</Card.Footer>
</Card>
</div>
)
)}
请帮忙。
<Card.Img>
是一个 img 标签,因此您应该使用 as 更新以更改默认元素:
<Card.Img as="div">
<img src={res.ImageUrl} width="400px" height="150px" />
</Card.Img>
并且您可以在调用 setState 之前添加一个变量来检查组件是否已安装或未安装
componentDidMount(){
this.mounted = true;
...
this.mounted && this.setState({loading: false, data: fetchedData});
...AbortController
}
componentWillUnmount(){
this.mounted = false
}
我正在尝试使用 React js 制作一个照片库应用程序。我将图像的 url 存储在 firebase 实时数据库中,但是当我尝试使用 img 标签显示这些图像时出现错误。
Error: img is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.
The above error occurred in the <img> component:
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
这就是我从 firebase
获取 url 的方式componentDidMount(){
axios.get('/Data.json')
.then(response => {
const fetchedData = [];
for(let key in response.data){
fetchedData.push({
...response.data[key],
id: key
});
}
this.setState({loading: false, data: fetchedData});
})
.catch(error => {
console.error(error)
this.setState({loading:false});
});
}
这就是我尝试显示图像的方式
this.state.data.reverse().map((res) => (
<div className="card1">
<Card
key={res.id}
style={{backgroundColor:"#343a40", textAlign:"left" ,
margin:"20px" ,color: "white",
left:"370px", borderRadius:"10px",
overflow:"hidden", width:"600px",
height:"200px", boxShadow:"0 10px 18px 0 rgba(0,0,0,0.2)"}}
>
<Card.Body
className="container">
<h4>
ANONYMOUS
</h4>
<Card.Text>
{res.Comment}
</Card.Text>
<Card.Img>
<img src={res.ImageUrl} width = "400px" height="150px" />
</Card.Img>
<Button className="btn btn-danger"
style={{float:"right", width:"40px"}}
onClick={() => this.DeleteCommentHandler(res.id)}>
<FontAwesomeIcon icon={faTrash}/>
</Button>
</Card.Body>
<Card.Footer>
{res.Date}
</Card.Footer>
</Card>
</div>
)
)}
请帮忙。
<Card.Img>
是一个 img 标签,因此您应该使用 as 更新以更改默认元素:
<Card.Img as="div">
<img src={res.ImageUrl} width="400px" height="150px" />
</Card.Img>
并且您可以在调用 setState 之前添加一个变量来检查组件是否已安装或未安装
componentDidMount(){
this.mounted = true;
...
this.mounted && this.setState({loading: false, data: fetchedData});
...AbortController
}
componentWillUnmount(){
this.mounted = false
}