reach / router

https://reach.tech/router
MIT License
6.9k stars 327 forks source link

Add an ability to bail out of rerenders #356

Open Hypnosphi opened 4 years ago

Hypnosphi commented 4 years ago

If I use Match, useMatch, or useParams, I actually want to react only to changes in the corresponding parts of location. But right now my components rerender on each location change, even if it's irrelevant.

One possible solution would be to put history into React context, and implement something like useLocationSelector(location => ...), similar to useSelector from React Redux. It would only rerender if selector returns a different value. Then, useMatch, useParams, and Match could be re-implemented on top of useLocationSelector. Something like useParam(paramName) would be also useful then: it would react to changes in the selected param only

I can open a PR if this idea makes sense to you.

A couple of links for more context: https://github.com/facebook/react/issues/15156 https://blog.isquaredsoftware.com/2018/11/react-redux-history-implementation/#v7-0

destroyeraseimprove commented 4 years ago

The similar question. What if I don't want to re-render whole layout but only update some component and url?