react useEffect报依赖报警告

react useEffect报依赖的警告问题

import React, { memo, useEffect, useState } from 'react'
import { getBanner } from '../../../../api-service/list/other'

const Recommend = memo(() => {
  const [banners,setBanners] = useState([])
  useEffect(()=>{
    ( async ()=>{
      // 网络请求,拿到轮播图数据
      const data  = await getBanner()
      setBanners(data?.banners)
      console.log(banners);
    })()
  },[])
  return (
    <div>Recommend 推荐div>
  )
})

export default Recommend

控制台警告详情

如果,加上数组依赖加上banners,就会死循环更新

src\pages\discover\c-pages\recommend\index.js
  Line 15:5:  React Hook useEffect has a missing dependency: 'banners'. Either include it or remove the dependency array  react-hooks/exhaustive-deps

Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.

WARNING in [eslint]
src\pages\discover\c-pages\recommend\index.js
  Line 15:5:  React Hook useEffect has a missing dependency: 'banners'. Either include it or remove the dependency array  react-hooks/exhaustive-deps

webpack compiled with 1 warning

已解决,我这里只所有报这个警告是因为在useEffect的函数里面有这么一句话

console.log(banners);,把一行删除就行了

https://blog.csdn.net/weixin_58576761/article/details/125693780

参考一下这个