Skip to content

Why is this.state not available by default? #5464

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

Closed
colllin opened this issue Nov 13, 2015 · 3 comments
Closed

Why is this.state not available by default? #5464

colllin opened this issue Nov 13, 2015 · 3 comments

Comments

@colllin
Copy link

colllin commented Nov 13, 2015

I'm happy to move this question elsewhere if you can point me in the right direction.

Why do I get an error (Uncaught TypeError: Cannot read property 'someProperty' of null) when I don't call this.setState() before the initial render?

render() {
    return (
        <span>{this.state.someProperty}</span>
    )
}

This error goes away if I simply call this.setState() with an empty object in componentWillMount:

componentWillMount() {
    this.setState({});
}

Isn't this.state part of the core React Component API? Why wouldn't it be present by default?

@fkling
Copy link
Contributor

fkling commented Nov 13, 2015

See #1851 .

@jimfb
Copy link
Contributor

jimfb commented Nov 13, 2015

Thanks @fkling! Closing this out in favor of #1851. Also, see #3236 (comment) which has a somewhat related discussion.

@colllin For future reference, usage questions are best addressed on StackOverflow. I know this was somewhat bug like, so this wasn't a bad place, but just something to consider; we try to keep github for tracking bugs in the core.

@jimfb jimfb closed this as completed Nov 13, 2015
@colllin
Copy link
Author

colllin commented Nov 13, 2015

Thank you, @fkling

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

3 participants