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 constructor docs to include context #168

Closed

Conversation

dustinsoftware
Copy link
Member

The docs previously did not indicate that it was necessary to call super(props, context) if you are using context within components, which could cause confusion. Opening this PR to start discussion around a change to make this more obvious (I searched and could not find a previous issue)

@reactjs-bot
Copy link

reactjs-bot commented Oct 16, 2017

Deploy preview ready!

Built with commit 12d8a2d

https://deploy-preview-168--reactjs.netlify.com

@gaearon
Copy link
Member

gaearon commented Oct 17, 2017

We intentionally don't use context in the API docs anywhere except the Context page because it's still considered experimental and people should minimize its direct usage. Even though we won't remove it we do plan to change it. Most people shouldn't attempt to use it directly.

It would make sense to me to instead add a caveat about this to the Context doc itself, and leave others as they are.

@dustinsoftware
Copy link
Member Author

dustinsoftware commented Oct 17, 2017 via email

@dustinsoftware
Copy link
Member Author

I looked, the page on context already has some documentation about the constructor.

Would you be open to adding a warning to the component docs page about avoiding using context directly, which links to the context docs page? It seems like since this page doesn't mention context in the constructor, it would be easy for someone to mistakenly exclude it, which would break using a library like react router, correct?

@gaearon
Copy link
Member

gaearon commented Oct 17, 2017

I'm open to adding a second "Note" in the constructor doc that mentions context linking to its doc and shows the different signature.

@EthanRutherford
Copy link

I would honestly recommend rest/spread-ing the constructor args.

class Foo extends Component {
    constructor(...args) {
        super(...args);
        // ...
    }
    // ...
}

A component author generally shouldn't be fiddling with any of the arguments to the constructor, and writing your constructors this way also future-proofs the component: if React team decides for whatever reason to radically change the constructor arguments, a constructor declared as above will be unaffected by the change. The same argument also applies to migrating a project into react-router, or any other framework which similarly uses context. Existing components should Just Work™️ regardless of the structure of the constructor arguments.

@@ -158,6 +158,10 @@ Beware of this pattern, as state won't be up-to-date with any props update. Inst

If you "fork" props by using them for state, you might also want to implement [`componentWillReceiveProps(nextProps)`](#componentwillreceiveprops) to keep the state up-to-date with them. But lifting state up is often easier and less bug-prone.

> Note
>
> Some libraries, such as React Router, may depend on [context](/docs/context.html) being available. If you implement a constructor and need context, make sure to call `super(props, context)` from the [constructor](/docs/context.html#referencing-context-in-lifecycle-methods).
Copy link
Member

Choose a reason for hiding this comment

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

This makes it sound like you need to use context directly to use React Router which isn't the case. RR ships with components and HOCs that hide the use of context. Can we reword to avoid this impression?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure thing.

@dustinsoftware
Copy link
Member Author

Ok, the wording has been updated

@bvaughn
Copy link
Contributor

bvaughn commented Nov 17, 2017

I'm a little uneasy about this wording. It's not technically required that you call super with context (or even props for that matter) since React has your back in case you forget. The only time it matters, in practice, is if you call this.context within the constructor function.

I'm wary that this change could influence people to bulk-modify their code unnecessarily in order to change super(props) to super(props, context) and that this could in turn make changes to context slightly less convenient down the road. (Although that kind of change would also be pretty easy to code mod away so maybe I'm just over-thinking it.)

I'm also a bit nervous that this note might confuse people unnecessarily (eg "How do I know if any of the libraries I'm using rely on context?") when in practice, it only matters in a very specific (and probably not super common) situation.

Either way, thanks so much for the proposal and the discussion. I could be wrong - and I'd love for people to speak up if they disagree with this decision - but I think it's best to leave this note out for now.

@bvaughn bvaughn closed this Nov 17, 2017
@dustinsoftware
Copy link
Member Author

dustinsoftware commented Nov 17, 2017 via email

jhonmike pushed a commit to jhonmike/reactjs.org that referenced this pull request Jul 1, 2020
BetterZxx pushed a commit to BetterZxx/react.dev that referenced this pull request Mar 21, 2023
…rived-state.md into Chinese (reactjs#168)

* 翻译完成

* 翻译改进

1 把译注放在文章下面
2 翻译之后的行和原文的行保证一致

* change format

* change title

* change some words

* change words

* delete some whitespaces

* fix commit error

* fix props
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.

6 participants