-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Use ReactDOM.unstable_batchedUpdates in useRouterHistory? #2919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Until it's sanctioned, I would avoid using these kinds of APIs. React already does batching internally and I believe improvements to negate the need for these kinds of extra APIs are coming. The side effects of using them might be pretty negative. Also, I'm getting $scope.apply angular flashbacks :P |
React only does batching right now inside React lifecycle hooks. |
Yeah, sorry, I should have said "does batching to a certain extent". But I'm wondering if this is something we should be concerning ourselves with. Who is connecting location state and for what reason? The router already provides that for you. So, you're getting the same information in two different ways. It's no wonder that you're getting rendered twice. The state that redux-simple-router provides is primarily useful outside of your components. Such as in action creators or middleware. So this shouldn't be a common problem. It's mainly a documentation of best practices by the binding libraries. |
Is Redux |
It has a shouldComponentUpdate check that does a shallow compare of the props. So it will only render when whatever it has selected changes. |
I see https://github.com/rackt/react-redux/blob/master/src/components/connect.js#L169-L181 and https://github.com/rackt/react-redux/blob/master/src/components/connect.js#L78-L80. Won't this always set |
Ah, I think you're right. It must be up to you to provide your own shouldComponentUpdate. I never did a deep dive on react-redux, I just normally focus on the main module. |
Or use reselect or something. But if we make React batch the updates in the |
We might have been the first project outside of facebook to use context, so we're not afraid of unstable API (it's projects like ours that often justify them). At the least, it could be an opt-in option to That said, I'd rather have a different solution. Is there a decent workaround? |
I don't think this should be an opt-in (or even opt-out) feature. It ought not break anyone or meaningfully change behavior except for preventing the double-render thing – it'd purely be an implementation detail on our side. The issue is basically:
Unless I'm misunderstanding, this issue is most prominent with Redux, since there's only a single store, so it causes every container to re-render. It's really a question of whether we're okay with calling into an unstable You can see in the Relay case that |
I still kind of expect a redux router integration that uses |
That's what redux-simple-router does. However, there are two listeners, and therefore two setState/renders. |
You'd actually have to do with redux-router does, which is to replace |
Just tested it – it doesn't actually help. Better probably to wait for React to update context propagation such that people no longer need to use |
@ryanflorence @taion I actually tried using the https://github.com/mjrussell/redux-router/blob/rr-v2/src/ReduxRouterContext.js This approach felt more like react-router was the single point of truth for the routing data and the Once I saw RSR had the full location data I hopped on that bandwagon though 😄 |
To clarify—this is correct but we're using a different technique to avoid rendering. We memoize the return value of previous |
Oh, I see. This is correct, with |
Well, we can't fix it just by using batched updates anyway. And mostly people can get by with pure Redux containers anyway. |
This only applies if you map location to props in Edit: |
@timdorr You need to set |
From reactjs/react-router-redux#197.
This issue arises when using a Flux integration for React Router. The issue is that, if the integration updates e.g. Flux stores when
history
updates, this will lead to a separatesetState
(in e.g. smart component containers, from the store update) from thesetState
in<Router>
(from the transition manager update directly), which will lead to the page rendering twice.We could in principle resolve this by using
ReactDOM.unstable_batchedUpdates
inuseRouterHistory
. We can't really do this inhistory
because that library is not aware of React. I think it'd make more sense to do it here rather than in every React Router integration, because otherwise they'd all need the same logic.The downsides of course are:
react-dom
dependency (for now) when currently there isn't onecc @gaearon, in case this is a horrible anti-pattern if used in conjunction with Redux
The text was updated successfully, but these errors were encountered: