Skip to content
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

5.0 Router does not provide React Context #6672

Closed
pickypg opened this issue Mar 28, 2019 · 3 comments
Closed

5.0 Router does not provide React Context #6672

pickypg opened this issue Mar 28, 2019 · 3 comments

Comments

@pickypg
Copy link

pickypg commented Mar 28, 2019

Version

5.0.0 (with React 16.8.5)

Test Case

TypeScript code:

import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';

import { BrowserRouter as Router } from 'react-router-dom';

class App extends React.Component {

  static contextTypes: React.ValidationMap<any> = {
    router: PropTypes.shape({
      history: PropTypes.shape({
        push: PropTypes.func.isRequired,
        createHref: PropTypes.func.isRequired
      }).isRequired
    }).isRequired
  }

  constructor(...args) {
    super(...args);

    // this will log "[{},{}]"
    console.log(JSON.stringify(args));
    // this will log "{}"
    console.log(JSON.stringify(this.context));
  }

  public render() {
    return (
      <p>Router {this.context.router ? 'defined' : 'is not defined'}</p>
    );
  }
}

// Wrap the `App` in the `Router` so that it provides it the `router` as context
ReactDOM.render(
  <Router>
    <App />
  </Router>,
  document.getElementById('root')
);

Steps to reproduce

Attempt to render the above page and observe that the router in not defined.

Expected Behavior

this.context.router to be defined.

Actual Behavior

this.context.router is not defined, which also causes warnings due to the contextTypes expecting it.

The exact same code works with react-router-dom 4.3.1.

@pshrmn
Copy link
Contributor

pshrmn commented Mar 28, 2019

Directly accessing the context was never part of the public API; the recommended way to access the context has always been to use withRouter.

@timdorr timdorr closed this as completed Mar 29, 2019
@pickypg
Copy link
Author

pickypg commented Mar 29, 2019

Well then, that explains it! Thanks for the quick response @pshrmn!

@lock lock bot locked as resolved and limited conversation to collaborators Jun 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants