2 ms·
This looks like dataflow programming to me and the big downside are the so called "glitches". We want to react to every change but at the same time we want some
by imtringued 7y ago
This looks like dataflow programming to me and the big downside are the so called "glitches". We want to react to every change but at the same time we want some changes to happen simultaneously.
const data = useMemo(() => (
expensive(a,b,c)
), [a,b,c])
Now image a,b,c all change at the same time in response to a button press. How often is expensive() going to be called? 3 times? That will take too long. 1 time? That's unpredictable.
- acemarke 7y agoReact batches updates that were queued from within its own event handlers. So, if you had: setA(1) setB(2) setC(3) in a single click handler, there would be a single render, and a single call to the `useMemo()` callback. On the other hand, if the state setters were called outside of a React event handler (such as a `setTimeout` or somewhere in an async function), each of those would cause a separate render pass, and the `useMemo` callback would end up being called 3 times. Note that in the upcoming Concurrent Mode, React will batch updates across event ticks by default.