-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Conversation
Deploy preview ready! Built with commit 12d8a2d |
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. |
Ok, I can make an update to that page then :)
…On Mon, Oct 16, 2017 at 17:04, Dan Abramov ***@***.***> wrote:
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#168 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA5hFmWvHHtLwdcdEBlHoMzBKZCF6awZks5ss-72gaJpZM4P7ZX2>
.
|
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? |
I'm open to adding a second "Note" in the constructor doc that mentions context linking to its doc and shows the different signature. |
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). |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing.
Ok, the wording has been updated |
I'm a little uneasy about this wording. It's not technically required that you call I'm wary that this change could influence people to bulk-modify their code unnecessarily in order to change 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 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. |
Ok, thanks for taking the time to reply. :)
…On Fri, Nov 17, 2017 at 08:04, Brian Vaughn ***@***.***> wrote:
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
<https://github.com/facebook/react/blob/b5e4b45a1036e3587415ae395f9d62a0229ed7e2/packages/react-reconciler/src/ReactFiberClassComponent.js#L446-L449>.
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#168 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA5hFoZ5EyaVPYkTYs-7EIi-MslQTNXJks5s3a5FgaJpZM4P7ZX2>
.
|
…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
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)