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

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

    just disable eslint for the next line;

    useEffect(() => {
       fetchBusinesses();
    // eslint-disable-next-line
    }, []);
    

    in this way you are using it just like a component did mount (called once)

    updated

    or

    const fetchBusinesses = useCallback(() => {
     // your logic in here
     }, [someDeps])
    
    useEffect(() => {
       fetchBusinesses();
    // no need to skip eslint warning
    }, [fetchBusinesses]); 
    

    fetchBusinesses will be called everytime someDeps will change

提交回复
热议问题