Re:Thinking in React Hooks
by foolbiter on Friday December 13, 2019 @12:31AM
(#21779345)
[ Parent
| on HN ]
Re:Thinking in React Hooks
by steve_taylor on Saturday December 14, 2019 @03:55AM
(#21788530)
I had no idea the setter can take a function instead of a value. It makes perfect sense, because setState has the same feature. TIL.
FWIW, it's documented in the API reference:
https://reactjs.org/docs/hooks-reference.html#functional-upd...
Re:Thinking in React Hooks
by andrewstuart on Friday December 13, 2019 @12:39AM
(#21779369)
Does this continuously clearInterval on every render?
Nope. Since the dep array is empty, the effect only runs once after the first render, & the cleanup function returned from the effect only runs on unmount.
Re:Thinking in React Hooks
by andrewstuart on Friday December 13, 2019 @03:52AM
(#21780108)
I like it. How did I miss this most obvious and simple solution?