5 ms·
One major point of the author is complaining that the dependency array in an effect is only compared by reference for objects and arrays. To me this is a featu
by littlecranky67 7y ago
One major point of the author is complaining that the dependency array in an effect is only compared by reference for objects and arrays.
To me this is a feature rather than a shortcomming: I use immutable data structures all the time (with array and object spread operators it is really easy to do so) and the effect will only update when it really needs to - thus only triggering a re-render (or rather reconciliation) when something has actually changed. This is in contrast to .setState() which triggered reconciliation regardless if a value actually changed.
- latortuga 7y agoThe example in the article can be fixed very easily and doesn't require immutable data structures. If you're fetching something from a url and you don't want to fetch twice from the same url, make the dependency the url itself.
- PKop 7y agoMore generally, one should be destructuring object properties (since he later did that anyways) at the top of the function. It's no more typing, but isolates property usage to one spot. Then pass them into the dependency array. Seems very convoluted to do that inside the useEffect, just to show how "it doesn't work". Especially because often a re-run of the effect will be desirable when some of the props change, not always the whole bag..