-
Notifications
You must be signed in to change notification settings - Fork 7.8k
16.3 release blog draft #587
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
Changes from 8 commits
444896e
141de66
ce57928
da29d89
a55480b
935d2a1
ab62e88
08d444e
5a1d653
8494428
c08533a
33ff167
c09a284
ee78358
b049931
9bc1932
6627687
0e7243f
c07a6b0
7b02740
d7b34d4
8075ce2
96fe82c
96b10a7
962ef9a
4b939d2
04c7f16
b1adba0
9b869fa
566406e
a89e6b8
5e0901d
3f304b9
f6e5d65
6e35810
6a1ba97
1de2e33
c7a61e6
fecb153
866ef01
12d7c16
e5bf594
2f97f48
a357b64
a937c46
201fcad
2b1de01
22d0ec1
fe033f6
f14f147
5d5a5d2
2ead10e
29d6592
0780f72
a5022da
fe3d0a4
808e5bd
de22379
291b2fd
6aa1995
7c6688c
b49f99b
dbce488
2f9b77a
e9ae68f
53d604f
fec6d6c
7b5764f
0d32f18
7db107b
da091e6
6eb29a1
9f54997
4e1282e
f588bd6
ccc1f26
3af89c9
01c2691
f5d5139
5864093
d66d3a2
ef23c49
b25276a
c41bb29
1ead060
d58c892
e689824
509b126
fe84e09
9e4442a
3c44882
f9225e2
fb8e9e8
6919801
c9d50e3
7ac0712
920b4f4
349446d
b0e8604
8437e37
587ad93
f11a149
bd06bbc
b7b97c0
6d77836
e5cf31a
a29098e
57eb7cc
28b2b76
691cd45
032576c
797a792
3a36a40
e5ca13e
992a756
5a587d1
ac1d767
aa9810f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
title: "React v16.3.0: New lifecycles and context" | ||
author: [bvaughn] | ||
--- | ||
|
||
This release includes a new class component lifecycle (`getDerivedStateFromProps`), a new `StrictMode` component, an official context API, and a new ergonomic ref API! | ||
|
||
For the past few months, the React team has been working on support for [asynchronous rendering](#). We are excited about the new features this will enable, and we've learned that some changes will be required to the way we write React components. | ||
|
||
We're releasing version 16.3 primarily so that open source maintainers can start incorporating these changes into their libraries well in advance of the next major release. **However, you should not feel pressured to make changes to your application code yet.** We respect semver and will not ship breaking changes in a minor version! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly this paragraph right here reads to me as a little alarmist since you haven't even told me what you're changing. Can we move this down into the lifecycle section maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some reassurance that we won't break anything in a minor release belongs near the top, where people are most likely to see it. Maybe I can shorten and soften the wording slightly to make it less alarmist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe something more like this: For the past few months, the React team has been working on support for asynchronous rendering. We are excited about the new features it will enable. We’ve also learned that some long-term changes will be required to the way we write React components. However, we respect semver and will not ship breaking changes in a minor version! |
||
|
||
Read on to learn more about the release. | ||
|
||
## Official Context API | ||
|
||
For many years, React has offered an experimental API for context. Although it was a powerful tool, our advice was mostly to [avoid using it](/docs/context.html#why-not-to-use-context) because of potential problems with the API. We've always intended to replace the experimental API with a better one, and as of version 16.3 the new API is available! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the why not context was actually focused on people using it as an alternative for props and it was discouraged because it can make code hard to follow, not because of its instability. Can we just say like "Although it was a powerful tool, its use was discouraged, in part because the API had inherent problems in its design from the start, and we always intended to replace the experimental API with a better one. In version 16.3, we're introducing a new context API that supports static type checking and deep updates as well as being more efficient." |
||
|
||
> **Note** | ||
> | ||
> The old context API will keep working for all React 16 minor releases, so you will have time to migrate. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "all React 16.x releases"? In case "minor" isn't universally clear. |
||
|
||
[Learn more about the new context API here.](#) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right. Thanks~ |
||
|
||
## `createRef` API | ||
|
||
Previously, React provided two ways for managing refs: the legacy string ref API and the callback API. Although the string ref API was the more convenient of the two, it had [several downsides](https://github.com/facebook/react/issues/1373) and so our official recomendation was to [use the callback form instead](https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs). | ||
|
||
Version 16.3 adds a new option for managing refs that offers the convenience of a string ref without any of the downsides: | ||
`embed:16-3-release-blog-create-ref.js` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still add
|
||
> **Note:** | ||
> | ||
> Callback refs will continue to be supported in addition to the new `createRef` API. | ||
> | ||
> You don't need to replace callback refs in your components. They are slightly more flexible, so they will remain as an advanced feature. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the upgrade path? Is there a codemod? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No codemod. I proposed one initially, but a codemod couldn't properly handle this case: class App extends React.Component {
render() {
return (
<OtherComponent
renderThing={() =>
<div ref='refThatShouldBelongToOtherComponentInstance' />
}
/>
);
}
} I guess we could still write a codemod that bailed out on that case, but it didn't seem like there was a lot of support for one when I mentioned it during last week's sync. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry if I was unclear; I am very supportive of the codemod that handles simple cases correctly, as long as it's solid in the bailouts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't thinking of you specifically, just the general impression I got was a lack of interest. I'm not necessarily volunteering to do it myself, but I'll add it to the quip as a non-16.3-release-blocking step. 😄 |
||
[Learn more about the new `createRef` API here.](#) | ||
|
||
## Component Lifecycle Changes | ||
|
||
React's class component API has been around for years with little change. However, as we add support for more advanced features (such as [error boundaries](/docs/react-component.html#componentdidcatch) and the upcoming [async rendering mode](#)) we stretch this model in ways that it was not originally intended. | ||
|
||
For example, with the current API, it is too easy to block the initial render with non-essential logic. In part this is because there are too many ways to accomplish a given task, and it can be unclear which is best. We've observed that the interrupting behavior of error handling is often not taken into consideration and can result in memory leaks—(something that will also impact the upcoming async rendering mode). The current class component API also complicates other efforts, like our work on a React compiler. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove em dash work on a React compiler -> "work on prototyping a React compiler"? don't want people to get hopes up too much. link it to https://twitter.com/trueadm/status/944908776896978946 too tho? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love it. |
||
|
||
Many of these issues are exacerbated by a subset of the component lifecycles (`componentWillMount`, `componentWillReceiveProps`, and `componentWillUpdate`). These also happen to be the lifecycles that cause the most confusion within the React community. For these reasons, we are going to deprecate those methods in favor of better alternatives. | ||
|
||
We recognize that this change will impact many existing components. (At Facebook, we maintain more than 50,000 React components, and we can't tell our engineers to rewrite them either.) Because of this, the migration path will be as gradual as possible, and will provide escape hatches. | ||
|
||
> **Note:** | ||
> | ||
> Deprecation warnings will be enabled in version 16.4, **but the legacy lifecycles will continue to work until version 17**. | ||
> | ||
> After version 17, it will still be possible to use them, but they will be aliased with an "UNSAFE_" prefix to indicate that they might cause issues. We have also prepared an [automated script to rename them](https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles) in existing code. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/After version 17/Even in version 17/ |
||
|
||
We are also adding a new static lifecycle, `getDerivedStateFromProps`, as a safer alternative to the legacy `componentWillReceiveProps`. | ||
|
||
[Learn more about these lifecycle changes here.](#) | ||
|
||
## `StrictMode` Component | ||
|
||
`StrictMode` is a tool for highlighting potential problems in an application. Like `Fragment`, `StrictMode` does not render any visible UI. It simply activates additional checks and warnings for its descendants. | ||
|
||
> **Note:** | ||
> | ||
> `StrictMode` checks are run in development mode only; they do not impact the production build. | ||
|
||
Although it is not possible for strict mode to catch all problems (e.g. certain types of mutation), it can help with many. If you see warnings in strict mode, those things will likely cause bugs for async rendering. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think mentioning "mutation" as a problem is going to seriously worry MobX users. What is our messaging here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure. I can soften this? ("certain types of mutations") |
||
In version 16.3, `StrictMode` helps with: | ||
* Identifying components with unsafe lifecycles | ||
* Warning about legacy string ref API usage | ||
* Detecting unexpected side effects | ||
|
||
Additional functionality will be added with future releases of React. | ||
|
||
[Learn more about the `StrictMode` component here.](#) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class MyComponent extends React.Component { | ||
// highlight-next-line | ||
divRef = React.createRef(); | ||
|
||
render() { | ||
// highlight-next-line | ||
return <input type="text" ref={this.divRef} />; | ||
} | ||
|
||
componentDidMount() { | ||
// highlight-next-line | ||
this.divRef.value.focus(); | ||
} | ||
} |
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.
s/some changes will be/some long-term changes will be/ maybe?