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

getDefaultProps not working #3655

Closed
meetwudi opened this issue Apr 13, 2015 · 7 comments · Fixed by #3675
Closed

getDefaultProps not working #3655

meetwudi opened this issue Apr 13, 2015 · 7 comments · Fixed by #3675

Comments

@meetwudi
Copy link

I am working on a very simple component with default props.

// React component
class Navigation extends React.Component {
  getDefaultProps() {
    return {
      user: {a:1}
    }
  }

  render() {
    console.log(this.props);
    if (this.props.user) {
      // User has been logged in
      return (
        ...
      );
    }
    else {
      // User not logged in
      return (
        ...
      );
    }
  }
}

Navigation.propTypes = {
  user: React.PropTypes.object
};

return Navigation;

And I simply render it without any props (<Navigation />). But I got an empty object from console output.

I am using react 0.13.1, chrome 41, OSX 10.10.

Any idea what is going wrong?

@iamdustan
Copy link
Contributor

Es6 classes need defaultProps as a static property.

Navigation.defaultProps = {...}

@meetwudi
Copy link
Author

Thanks. Maybe update this in documentation :) ? @iamdustan I will be happy to send in a PR

@iamdustan
Copy link
Contributor

#3647 has some commentary on docs and es6. I agree that I’d like to see better docs on the class syntax.

I’m just a guy who happened to see your issue really quickly :)

@zpao
Copy link
Member

zpao commented Apr 13, 2015

http://facebook.github.io/react/docs/reusable-components.html#es6-classes does reference this. It might be good to also put some documentation in http://facebook.github.io/react/docs/component-specs.html#getdefaultprops to make it a bit easier to find. I'm going to close this out since it's invalid but I'd love to see a PR with more docs :)

@zpao zpao closed this as completed Apr 13, 2015
@sophiebits
Copy link
Collaborator

I think you should have also seen a runtime warning for this?

@meetwudi
Copy link
Author

@spicyj not really, though

@sophiebits
Copy link
Collaborator

@tjwudi Thanks, looks like an oversight. Fix incoming…

@sophiebits sophiebits reopened this Apr 16, 2015
sophiebits added a commit to sophiebits/react that referenced this issue Apr 16, 2015
sophiebits added a commit that referenced this issue Apr 16, 2015
Add warning for getDefaultProps on ES6 classes
zpao pushed a commit to zpao/react that referenced this issue Apr 16, 2015
Add warning for getDefaultProps on ES6 classes
zpao pushed a commit to zpao/react that referenced this issue Apr 17, 2015
Add warning for getDefaultProps on ES6 classes
zpao pushed a commit to zpao/react that referenced this issue Apr 18, 2015
Add warning for getDefaultProps on ES6 classes
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

Successfully merging a pull request may close this issue.

4 participants