小编Ang*_*sty的帖子

Next JS 在状态改变时不会重新渲染元素

我有一个代码,我可以在其中获取图像名称

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)

但问题是,当获取图像名称时,组件不会重新渲染。所以现在还是空的。我怎样才能在获取返回数据时渲染该图像?

reactjs next.js

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

next.js ×1

reactjs ×1