In this solution useEffect will trigger after the first render, so we will see text in the console without any typing. If we want to fetch some data "onChange" then we will have an extra redundant request.
Maybe a better solution will be to move setTimeout to handleOnChange function?
Thank you for the comment! I a gree with the "trigger after the first render" issue.
However if we move setTimeout to handleOnChange we will have to take care of handling clear timeout manually (useEffect do it for us with the return callback fn).
The other thing is that logging to the console / fetching some data etc. are side effects to value changes - useEffect seems like a perfect choice here.
I would rather add some extra logic inside useEffect (e.g. using useRef) to make sure that our effect runs only after the second and every subsequent render. What do you think?
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In this solution useEffect will trigger after the first render, so we will see text in the console without any typing. If we want to fetch some data "onChange" then we will have an extra redundant request.
Maybe a better solution will be to move setTimeout to handleOnChange function?
Thank you for the comment! I a gree with the "trigger after the first render" issue.
However if we move
setTimeout
tohandleOnChange
we will have to take care of handling clear timeout manually (useEffect
do it for us with the return callback fn).The other thing is that logging to the console / fetching some data etc. are side effects to
value
changes -useEffect
seems like a perfect choice here.I would rather add some extra logic inside
useEffect
(e.g. usinguseRef
) to make sure that our effect runs only after the second and every subsequent render. What do you think?