React Hook useEffect has a missing dependency: 'list'

前端 未结 5 2132
一个人的身影
一个人的身影 2021-01-04 20:27

Once I run the below code, I get the following error:

React Hook useEffect has a missing dependency: \'list\'. Either include it or remove the depende

相关标签:
5条回答
  • 2021-01-04 20:52

    Inside your useEffect you are logging list:

    console.log(list);
    

    So you either need to remove the above line, or add list to the useEffect dependencies at the end. so change this line

    }, [term]);
    

    to

    }, [term, list]);
    
    0 讨论(0)
  • 2021-01-04 20:54

    This warning comes beacuse you have a state 'list' being used inside useEffect. So react warns you that 'list' is not added to dependency so any changes to 'list' state won't trigger this effect to run again.
    You can find more help here

    0 讨论(0)
  • 2021-01-04 20:55

    You can disable this by inserting a comment

    // eslint-disable-next-line

    0 讨论(0)
  • 2021-01-04 20:57

    You can disable the lint warning by writing:

    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [term]);
    
    0 讨论(0)
  • 2021-01-04 21:12

    The dependency array - it's the second optional parameter in the useEffect function. React will recall function defined in the first parameter of useEffect function if parameters inside dependency array will changed from the previous render.

    Then you doesn't need to place list variable inside dependency array.

      useEffect(() => {
        // do some
    
        // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [term]);
    
    0 讨论(0)
提交回复
热议问题