Export context to allow history access
See original GitHub issueRef https://github.com/ReactTraining/react-router/issues/6362#issuecomment-425841471
Since “always render” behaviour will be removed from <Route children /> it would be good to provide a way to access context values from any part of app.
Here’s a few ways and all requires to provide RouterContext.
RouterContext.Consumer
const Component = () => (
<RouterContext.Consumer>
{context => (
<button onClick={() => {
if (context) {
context.history.push('/users')
}
}}>Open users</button>
)}
</RouterContext.Consumer>
)
Class component contextType
const Component = class extends React.Component {
contextType = RouterContext;
handler = () => {
this.context.history.push('/users')
}
}
context.unstable_read api
const Component = () => {
const context = RouterContext.unstable_read();
return (
<button onClick={() => {
if (context) {
context.history.push('/users')
}
}}>Open users</button>
)
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:13 (12 by maintainers)
Top Results From Across the Web
Export context to allow history access · Issue #6365 - GitHub
Here's a few ways and all requires to provide RouterContext . RouterContext.Consumer. const Component = () => ...
Read more >How to push to History in React Router v4? - Stack Overflow
You can use the history methods outside of your components. Try by the following way. First, create a history object used the history...
Read more >Export Content search results - Microsoft Purview (compliance)
Export the search results from a Content search in the Microsoft Purview compliance portal to a local computer. Email results are exported ......
Read more >Search History and My Exports - SeekOut | Help Center
Access your search history and previous exports by clicking your name at the bottom left of the dashboard and selecting Search History.
Read more >How to Work with the React Context API - Toptal
React's context allows you to share information to any component, by storing it in a central place and allowing access to any component...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I think the goal should be to eventually drop
withRouter()and only useRouterContext.Edit: To elaborate, I think of a context consumer (read from) as being public, while a provider (write to) is private. A
<Route>is a bit unusual since it is both a consumer and a provider. A pathless<Route>(outside of a<Switch>) is only being used as a consumer, so it is just adding complexity when a user really just wants to read from the context.Exposing the consumer will also let users use whatever future context optimizations for class components there are (
Context.read(),static contextType, etc.). The current pattern of using a wrapper around classes that need access to the context in lifecycle methods is tedious at best.@mjackson AFAIK exporting context is going to be normal/expected with what’s coming. Formik is going to do the same.