use-http icon indicating copy to clipboard operation
use-http copied to clipboard

Eslint rule react-hooks/exhaustive-deps questions on having response as depency

Open iqoOopi opened this issue 2 years ago • 1 comments

          > Try passing `response` into `useEffect`

Hi @alex-cory , got the same question. I'm wondering why adding response to useEffect/useCallback dependency won't cause re-render?

Let's say I have three api calls inside the componet

 const { loading, response, get, put } = useFetch();
  const call1= useCallback(async () => {

    const data= await get('endpoint1');
    if (response.ok) {
         
    }
  }, [get, response]);

  const call2= useCallback(async () => {

    const data= await get('endpoint2');
    if (response.ok) {
         
    }
  }, [get, response]);

  useEffect(() => {
    call2();
  }, [call2]);

when Call1 fired, the response will be changing, thus will cause a re-render on call2 thus then useEffect will be called again?

Originally posted by @iqoOopi in https://github.com/ava/use-http/issues/87#issuecomment-1433429198

iqoOopi avatar Feb 16 '23 17:02 iqoOopi

try passing response.ok to dependency array

alex-cory avatar Jun 15 '23 14:06 alex-cory