【发布时间】:2019-07-01 07:01:40
【问题描述】:
最初存在图像循环问题(它正在杀死我们的 api 调用)并意识到我们需要删除 componentDidUpdate。现在,当使用 React Dev Tools 检查页面时,我可以看到除了 img src 更新(包括 alt 标签)之外的所有内容。如果没有向页面呈现任何内容,那么它将正确呈现信息,但是一旦出现某些内容,它就会改变一切,除了图像本身。
class Img extends Component {
constructor(props) {
super(props)
this.state = {
src: ""
}
}
// componentDidUpdate() {
// this.getImageId();
// }
componentDidMount() {
this.getImageId();
}
getImageId() {
axios.get(`http://localhost:4000/api/images/${this.props.src}`)
.then(response => {
console.log(response.data) //this shows only on first request
this.setState({
src: response.data
})
})
.catch(err => console.log(err))
}
render() {
return ( <img src = {
this.state.src
}
alt = {
this.props.alt
}
style = {
{
height: "150px",
width: "200px",
borderRadius: "10px"
}
}
/>
)
}
}
【问题讨论】:
-
理论上,为什么您需要多次获取图像?
-
图像数量不是问题,因为一旦游戏或游戏列表第一次渲染,除了游戏封面图像之外的所有内容都发生变化。
-
解决方案还能用吗?