When will this useEffect work?

  • 0
    In the code below, useEffect in dependencies stores a link to the handler, as I understand it will be constant, due to the fact that the handler is wrapped in useCallback, i.e. the handler seems to never change, then what's the point of specifying it in the dependency array?
    const handler = useCallback(() => { 
    // что-то сделать 
    }, []) 
    useEffect(() => {
     handler(value) 
    // если не использовать useCallback, эффект будет срабатывать постоянно 
    },
     [handler, value])
    React Anonymous, Jan 23, 2020

  • 1 Answers
  • 0
    When will this useEffect work?


    When value or handler will change



    What's the point of specifying it in the dependency array?


    If the code will never, ever change, it will be printed on gold plate and sent into space then it makes no sense.

    If the code is maintained and changed, then in the future a situation may arise when dependencies appear in useCallback, that is, the handler will change. But useEffect won't know about it and a bug will appear.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!