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

Update tutorial to match class component docs #9990

Closed
wants to merge 2 commits into from

Conversation

ccardinaux
Copy link

According to the docs, class components should always call the base constructor with props. Update the tutorial to reflect this.

According to the docs, class components should always call the base constructor with props. Update the tutorial to reflect this.
https://facebook.github.io/react/docs/state-and-lifecycle.html#adding-local-state-to-a-class
@@ -216,7 +216,7 @@ class Square extends React.Component {
}
```

In [JavaScript classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), you need to explicitly call `super();` when defining the constructor of a subclass.
In [JavaScript classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes), you need to explicitly call `super();` when defining the constructor of a subclass. When creating a class component in React, you should always call the base constructor with `props`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of:

When extending a class component in React, always call the base constructor with props.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't true though. Passing props to super is only required when access this.props in the constructor, which is pointless since you can just access props from the constructor args.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's true. It may not be necessary, but excluding props also might still be outside of the intended contract for the React.Component constructor. Other docs do state that props should always be passed to super.

So there's inconsistency here. It might not matter, but I'm curious for @gaearon's take here. Is calling super() without passing props problematic for present or future versions of React?

If a hard fast rule of "always all React.Component.constructor with props" isn't true, we should take a pass to clear up the ambiguity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be pedantic, but this shows up again in the API documentation for React.Component.prototype.constructor:

https://facebook.github.io/react/docs/react-component.html#constructor

When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.

Copy link
Contributor

@nhunzaker nhunzaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just a suggestion about phrasing. What do you think?

@ccardinaux
Copy link
Author

Thanks for the review, @nhunzaker - agree with your suggested phrasing.

Copy link
Contributor

@nhunzaker nhunzaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good on my end, but I'd like to hear back about whether or not we want to continue to formally mandate props be passed to super. As @milesj as mentioned, this isn't technically necessary.

@sebmarkbage Any thoughts? When a class extends from React.Component, should it always call super(props)? Is omitting props a breach of contract?

@sebmarkbage
Copy link
Collaborator

The reason you might want to ensure that props is passed to super is for the field initializer use case:

class Foo extends Component {
  state = { value: this.props.defaultValue };
  constructor(props) {
    super(props);
  }
}

However, if you have field initializers you shouldn't need the constructor at all.

@sophiebits
Copy link
Collaborator

I think we decided our Flow typings would mandate passing props up though so maybe we should do it in the example too. I'm supportive unless @sebmarkbage thinks this is worse.

@sebmarkbage
Copy link
Collaborator

Seems fine to me.

@gaearon
Copy link
Collaborator

gaearon commented Sep 11, 2017

We'd also need to update the fiddles.. sigh

@bvaughn
Copy link
Contributor

bvaughn commented Oct 8, 2017

Thank you for filing this PR!

I'm sorry to be the bearer of bad news, but the documentation and source code for reactjs.org now lives in a different repository: reactjs/reactjs.org. (For more info on why we made this move, see issue #11075.)

It looks like the change to explicitly show super(props) called in the example was already made in the new repo via reactjs/react.dev/pull/8 😄

@bvaughn bvaughn closed this Oct 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants