How to fix missing dependency warning when using useEffect React Hook?

前端 未结 13 1834
无人及你
无人及你 2020-11-22 03:14

With React 16.8.6 (it was good on previous version 16.8.3), I get this error when I attempt to prevent an infinite loop on a fetch request

./src/components/Bu         


        
13条回答
  •  别那么骄傲
    2020-11-22 03:24

    const [mount, setMount] = useState(false)
    const fetchBusinesses = () => { 
       //function defination
    }
    useEffect(() => {
       if(!mount) {
          setMount(true);
          fetchBusinesses();
       }
    },[fetchBusinesses]);
    

    This is solution is pretty simple and you don't need to override es-lint warnings. Just maintain a flag to check whether component is mounted or not.

提交回复
热议问题