π‘ It turns out that the browser history can also be considered as an external data source.
React-Router (v5) provides us with everything we need to create a custom useHistorySelector() hook
The implementation is quite simple:
Now, when you click on a hash link above, the CurrentPathname component will not re-render anymore!
Using useSyncExternalStore() enables you to create your own selector system, returning only what you need to read in the React component
This prevents useless React re-renders!
There are so many external data sources that we can subscribe to, and implementing your own selector system on top might enable you to optimize React re-renders.
For example, let's consider we want to read the scrollY position of a page
We can implement this custom React hook:
Scroll the page and see how the components above re-render?
One re-renders much less!
π‘ Returning a narrower value also prevents useless React re-renders
"Mutability has great positives
But thinking immutability is bad may just mean some past patterns were problematic, and not that the entirety of immutable data is bad"
I can agree, but maybe the pain is only temporary?
Maybe we shouldn't throw the immutability model because of short-term difficulties, and the platform should evolve to support better the immutable UI model