-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
using the new context provider from react 16.3 breaks the router #6072
Comments
It looks like this is only an issue if the router and context provider elements are created in the same render call. It isn't immediately clear in my mind why this is the case. // does not work
const One = () => (
<Router>
<Context.Provider>
<Route />
</Context.Provider>
</Router>
);
ReactDOM.render(<One />, ...);
// does not work
const Two = () => (
<Route />
);
ReactDOM.render((
<Router>
<Context.Provider>
<Two />
</Context.Provider>
</Router>
), ...);
// works
const Three = () => (
<Context.Provider>
<Route />
</Context.Provider>
);
ReactDOM.render((
<Router>
<Three />
</Router>
), ...); Edit Sandbox demo https://codesandbox.io/s/oo6xmjo936 |
Might be related? facebook/react#12551 |
Will track via the React issue. For the time being, anyone running into this issue should just render the router and context provider separately. |
I'm having an issue with Routes not updating when my hash/path changes. Which may be this issue. However while I am using the new context system, I do not think this is an issue specific to the new context system. As I see the following in the script. The old context system was never reliable enough to expect that context would be updated, the moment you try to use PureRender/shouldComponentUpdate to make your application run efficiently the old context system falls apart. Everyone using the old context system was expected to only pass static data down through context, if dynamic data was desired the accepted pattern was to pass down some sort of static observer so the consumers could subscribe instead of expecting context to be updated. Router.prototype.getChildContext = function getChildContext() {
return {
router: _extends({}, this.context.router, {
history: this.props.history,
route: {
location: this.props.history.location,
match: this.state.match
}
})
};
}; I do not believe this is something that is React's job to fix, |
@dantman See this for a better explanation of what's going on: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md |
@timdorr Sounds exactly like what I thought. This is a react-router bug, the update behaviour of the old context API should never have been relied on. react-router should have been passing a subscriber down or using a 3rd party context wrapper that does so. Not expecting the old context API to update data and writing a long explainer on how to workaround the issue. I'm not going to pepper my codebase with workarounds because react-router is not using context correctly. I guess I'm just going to have to abandon react-router until it starts using the new context API. |
Unfortunately this is what we had to do in our large application. We did not realize until too late in the refactor to the latest RR version that this was a problem. Luckily we were able to make a HOC that we swapped out all Link components with that uses redux to do this instead. |
@brianespinosa Can you please share with us how you´ve solved that ? I need to use the context provider with routing without success. |
@renatonmendes we had to create our own Webpack plugin that replaces our use of the Past this explanation, unfortunately I cannot share any code with you from my current project. |
Hi, |
Should be fixed in React 16.4. |
This problem still exists in my project (reacta version 16.4). It occurs in such a case:
|
Can you provide a new small reproducing case? Ideally without any third party libs, just React. |
This should be fixed in 16.4, so I'm closing this out. |
@szagi3891 did you add withRouter to your Context Component? That solved it for me with 16.4 |
@Martsyalis just like this?
Does not work for me actually >.> |
@Martsyalis
Then the react-router started to work properly. |
withRouter should be used on every component that relies on history or match props. So everything that listens to url. |
Version
4.2.2
Test Case
https://codesandbox.io/s/rjl08qoxlp
Steps to reproduce
Use a context provider inside the router component
Expected Behavior
Using the new context provider should not break the router.
Actual Behavior
Using the new context provider is breaking the router.
The text was updated successfully, but these errors were encountered: