我有一个代码,我可以在其中获取图像名称
const [foundImg, setFoundImg] = useState(false)
var imgNames = []
---
axios.get('http://someapi.com')
.then(res => {
imgNames.push(res.data)
setFoundImg(true)
}.catch(err => console.log(err)
Run Code Online (Sandbox Code Playgroud)
我把这个条件放在返回/渲染中
{ (foundImg === true)
? itemImg.map( i => {
return <div className="product--box">
<Image width={50} height={50} src='/public/dummy.png' className='img'/>
</div>
})
: null
}
Run Code Online (Sandbox Code Playgroud)
但问题是,当获取图像名称时,组件不会重新渲染。所以现在还是空的。我怎样才能在获取返回数据时渲染该图像?