News for hackers. Stuff that matters.
Re:Thinking in React Hooks by foolbiter on Friday December 13, 2019 @12:31AM (#21779345)

  function Counter() {
    const [counter, setCounter] = React.useState(0);
    React.useEffect(() => {
      const interval = setInterval(() => setCounter(n => n + 1), 100);
      return () => clearInterval(interval);
    }, [])

    return (
      <div>{counter}</div>
    );
  }
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.
Re:Thinking in React Hooks by acemarke on Saturday December 14, 2019 @08:56PM (#21793935)
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?
Re:Thinking in React Hooks by foolbiter on Friday December 13, 2019 @01:45AM (#21779624)
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?