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

context undefined while use ES6 constructor #6598

Closed
imshenyz opened this issue Apr 24, 2016 · 7 comments
Closed

context undefined while use ES6 constructor #6598

imshenyz opened this issue Apr 24, 2016 · 7 comments

Comments

@imshenyz
Copy link

All issue in the code:

import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, browserHistory } from 'react-router'

export default class App extends Component {
    // If use constructor below this.context is undefined, 
    // otherwise comment the constructor statement this.context not null
    // Uncaught TypeError: Cannot read property 'router' of undefined
    router = this.context.router;
    constructor (props) {
        super(props);
        console.log(this);
    }
    render () {
        return <span>ReactDemo</span>
    }
    static contextTypes = {
        router: PropTypes.object.isRequired
    };
}

ReactDOM.render((
    <Router history={browserHistory}>
        <Route path="/" component={App}/>
    </Router>
), document.getElementById('root'));

The react versioin is 15.0.1,i remember a previous version without this problem.
Is this a bug or the usage of my problem?
Here is my demo git:
https://github.com/shenyzore/react-demo.git
Thanks!

@kalaxiya
Copy link

you should pass the second param:

constructor(props, context) {
// context can be accessed now
}

@imshenyz
Copy link
Author

@Linzerui it works, thanks a lot!

@depoulo
Copy link

depoulo commented Aug 22, 2016

Is this documented anywhere? I just went looking under https://facebook.github.io/react/docs/ but eventually got here via Google because I couldn't find it there...

@imshenyz
Copy link
Author

@depoulo I think there is no document for this, but you can inspired by the usage of context-in-stateless-functional-components

@depoulo
Copy link

depoulo commented Aug 22, 2016

Which is far from ideal, but thx anyway!

@JanMouwes
Copy link

Update for 16.x: passing context to constructor is not documented in 'current' documentation, I did find it in the legacy-documentation though. This leads me to believe it is deprecated.

According to the new documentation in the section that links to the legacy-docs: "The old API will be supported in all 16.x releases, but applications using it should migrate to the new version. The legacy API will be removed in a future major React version." (Found here)

@MilanVilov
Copy link

you should pass the second param:

constructor(props, context) {
// context can be accessed now
}

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants