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

前端 未结 13 1998
无人及你
无人及你 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:38

    This article is a good primer on fetching data with hooks: https://www.robinwieruch.de/react-hooks-fetch-data/

    Essentially, include the fetch function definition inside useEffect:

    useEffect(() => {
      const fetchBusinesses = () => {
        return fetch("theUrl"...
          // ...your fetch implementation
        );
      }
    
      fetchBusinesses();
    }, []);
    

提交回复
热议问题