question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Export context to allow history access

See original GitHub issue

Ref 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
pshrmncommented, Oct 1, 2018

I think the goal should be to eventually drop withRouter() and only use RouterContext.

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.

1reaction
jaredpalmercommented, Oct 1, 2018

@mjackson AFAIK exporting context is going to be normal/expected with what’s coming. Formik is going to do the same.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found