V
V
Vann Damm2020-11-19 10:38:57
React
Vann Damm, 2020-11-19 10:38:57

In what cases will this useEffect work?

In the code below, useEffect in dependencies stores a reference to handler , as I understand it, 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])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2020-11-19
@effect_tw

In what cases will this useEffect work?

When value or handler changes
What is the point of specifying it in the dependency array?

If the code will never, ever change, will be printed on a gold plate and sent into space , then there is no point.
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. And useEffect will not know about it and a bug will arise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question