-
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
Merged
Merged
Changes from 48 commits
Commits
Show all changes
118 commits
Select commit
Hold shift + click to select a range
444896e
Initial draft for 16.3 release blog
bvaughn 141de66
Wording changes in response to PR feedback
bvaughn ce57928
Added entry for createRef API
bvaughn da29d89
Added note about string ref API to strict mode section
bvaughn a55480b
Small wording nit
bvaughn 935d2a1
Minor edits in response to PR feedback
bvaughn ab62e88
Formatted example
bvaughn 08d444e
Wordsmithing in response to PR feedback
bvaughn 5a1d653
Incorporated Sophie's PR feedback
bvaughn 8494428
Added example of before/after context API
bvaughn c08533a
Removed context-before example
bvaughn 33ff167
Wordsmith nit
bvaughn c09a284
Added React.createRef doc
trueadm ee78358
address feedback
trueadm b049931
revised content as per PR feedback
trueadm 9bc1932
updated line numbers
trueadm 6627687
Describe what a ref is at the top of the doc
0e7243f
Merge intro paragraphs
c07a6b0
Merge pull request #1 from alexkrolick/create-ref-patch
trueadm 7b02740
Create "Accessing refs" section above specific examples
d7b34d4
Merge pull request #2 from alexkrolick/create-ref-patch-2
trueadm 8075ce2
Make the callback example more similar to the new API
96fe82c
Declare setRef method outside render
alexkrolick 96b10a7
Grammar
alexkrolick 962ef9a
Highlight deprecation notice
alexkrolick 4b939d2
~~All things considered~~
alexkrolick 04c7f16
Wording
alexkrolick b1adba0
Recommend old API when applicable
alexkrolick 9b869fa
Update leading sentence
alexkrolick 566406e
Add docs for new Context API (R16.3)
alexkrolick a89e6b8
Added link to context API docs
bvaughn 5e0901d
Merge pull request #3 from alexkrolick/create-ref-patch-3
trueadm 3f304b9
Merge pull request #4 from alexkrolick/create-ref-patch4
trueadm f6e5d65
Fixed off-by-one line highlights.
bvaughn 6e35810
Merge branch 'master' into 16.3-release-blog-post
bvaughn 6a1ba97
Moved 16.3 release date arbitrarily forward
bvaughn 1de2e33
Fixed links
bvaughn c7a61e6
Imported StrictMode docs (and examples) from PR #596
bvaughn fecb153
Merge pull request #1 from trueadm/create-ref
bvaughn 866ef01
Prettier
bvaughn 12d7c16
Updated createRef 'value' attributes to 'current'
bvaughn e5bf594
Unnest callback ref example code
alexkrolick 2f97f48
Fixed docs
bvaughn a357b64
Added React.forwardRef to 16.3 release blog post
bvaughn a937c46
Typo fixes
bvaughn 201fcad
Added React.forwardRef to API and docs
bvaughn 2b1de01
Rebuild Netlify
bvaughn 22d0ec1
Updated forwardRef example to use named function
bvaughn fe033f6
Added StrictMode to docs sidenav
bvaughn f14f147
Renamed ref in example
bvaughn 5d5a5d2
StrictMode title => Strict Mode
bvaughn 2ead10e
Update nav
gaearon 29d6592
Update React.Component reference to show deprecations and new lifecycles
bvaughn 0780f72
Reorganize examples & legacy docs
alexkrolick a5022da
Add in-page TOC
alexkrolick fe3d0a4
Typo
alexkrolick 808e5bd
Rephrase and reorganize
alexkrolick de22379
Syntax tweaks
alexkrolick 291b2fd
Wording tweaks
alexkrolick 6aa1995
Add solution to motivation problem
alexkrolick 7c6688c
Highlight createContext()
alexkrolick b49f99b
Highlight whole createContext() call
alexkrolick dbce488
Merge pull request #3 from alexkrolick/new-context-api
bvaughn 2f9b77a
16.4 -> 16.x
bvaughn e9ae68f
remove confusing {}
alexkrolick 53d604f
~deprecated~ legacy
alexkrolick fec6d6c
Add mutliple contexts example
alexkrolick 7b5764f
Add ref forwarding, lifecycle examples
alexkrolick 0d32f18
Kill highlight
alexkrolick 7db107b
+ "default value" comment
alexkrolick da091e6
Add note about how changes are determined
alexkrolick 6eb29a1
Combine basic examples
alexkrolick 9f54997
Update highlights
alexkrolick 4e1282e
Add to "when to use context"
alexkrolick f588bd6
Update "legacy" wording
alexkrolick ccc1f26
Replace "deprecated" with "legacy" for now
alexkrolick 3af89c9
Merge pull request #4 from alexkrolick/context-tweaks
bvaughn 01c2691
Replaced cWRP in forwardRef example with cDU
bvaughn f5d5139
Formatted a Note
bvaughn 5864093
Updated context-in-lifecycles example
bvaughn d66d3a2
Updated TODO for later
bvaughn ef23c49
Updated multi-context example to show more indirection
bvaughn b25276a
Updated forward-refs example to use class component
bvaughn c41bb29
Added DevTools name consideration to forwardRef examples
bvaughn 1ead060
17 -> future version
alexkrolick d58c892
+ "same algo"
alexkrolick e689824
Add notes about composing multiple contexts
alexkrolick 509b126
Explain context in lifecycle a bit more
alexkrolick fe84e09
Explain why you need forwardRef
alexkrolick 9e4442a
typo
alexkrolick 3c44882
Use function keyword
alexkrolick f9225e2
Remove propTypes
alexkrolick fb8e9e8
kill class props
alexkrolick 6919801
Use functional component
alexkrolick c9d50e3
Remove lib link
alexkrolick 7ac0712
Updated 16.3 blog post to account for getSnapshotBeforeUpdate lifecycle
bvaughn 920b4f4
Replace divs with some fake components
alexkrolick 349446d
Add caveats section
alexkrolick b0e8604
Prettier
alexkrolick 8437e37
Fix syntax
alexkrolick 587ad93
Merge pull request #5 from alexkrolick/16.3-release-blog-post
bvaughn f11a149
Added DevTools naming consideration to React.forwardRef reference doc…
bvaughn bd06bbc
Added context HOC example to reference
bvaughn b7b97c0
Merge branch 'master' into 16.3-release-blog-post
bvaughn 6d77836
Linked to async blog post
bvaughn e5cf31a
Renamed blog post
bvaughn a29098e
Merge branch 'master' into 16.3-release-blog-post
bvaughn 57eb7cc
Tweaked message about what goes into state
bvaughn 28b2b76
Tweaked state-and-lifecycle wording more
bvaughn 691cd45
Added inline child function caveat to Context docs
bvaughn 032576c
Prettier
bvaughn 797a792
Revert "Prettier"
bvaughn 3a36a40
Revert "Added inline child function caveat to Context docs"
bvaughn e5ca13e
Cross-link to async update post
bvaughn 992a756
Missing 'we'
bvaughn 5a587d1
Tweaked link to Context API
bvaughn ac1d767
Fixed link
bvaughn aa9810f
Fixed some links and improved a recommendation
bvaughn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| --- | ||
| title: "React v16.3.0: New lifecycles and context API" | ||
| author: [bvaughn] | ||
| --- | ||
|
|
||
| This release includes a new class component lifecycle (`getDerivedStateFromProps`), a new `StrictMode` component, an official context API, a new ergonomic ref API, and a ref-forwarding API! | ||
|
|
||
| For the past few months, the React team has been working on support for [asynchronous rendering](/blog/2018/03/01/sneak-peek-beyond-react-16.html). 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](https://semver.org/) 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, its use was discouraged because of inherent problems in the API, and because we always intended to replace the experimental API with a better one. | ||
|
|
||
| Version 16.3 introduces a new context API that is more efficient and supports both static type checking and deep updates. | ||
|
|
||
| > **Note** | ||
| > | ||
| > The old context API will keep working for all React 16.x releases, so you will have time to migrate. | ||
|
|
||
| Here is an example illustrating how you might inject a "theme" using the new context API: | ||
| `embed:16-3-release-blog-post/context-example.js` | ||
|
|
||
| [Learn more about the new context API here.](/docs/context.html#api) | ||
|
|
||
| ## `createRef` API | ||
|
|
||
| Previously, React provided two ways of 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 recommendation was to use the callback form instead. | ||
|
|
||
| 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-post/create-ref-example.js` | ||
|
|
||
| > **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. | ||
|
|
||
| [Learn more about the new `createRef` API here.](/docs/refs-and-the-dom.html) | ||
|
|
||
| ## `forwardRef` API | ||
|
|
||
| [Higher-order components](/docs/higher-order-components.html) (or HOCs) are a common way to reuse code between components. Building on the theme context example from above, we might create an HOC that injects the current "theme" as a prop: | ||
|
|
||
| `embed:16-3-release-blog-post/hoc-theme-example.js` | ||
|
|
||
| We can use the above HOC to wire components up to the theme context without having to use `ThemeContext` directly. For example: | ||
|
|
||
| `embed:16-3-release-blog-post/fancy-button-example.js` | ||
|
|
||
| HOCs typically [pass props through](/docs/higher-order-components.html#convention-pass-unrelated-props-through-to-the-wrapped-component) to the components they wrap. Unfortunately, [refs are not passed through](/docs/higher-order-components.html#refs-arent-passed-through). This means that we can't attach a ref to `FancyButton` if we use `FancyThemedButton`- so there's no way for us to call `focus()`. | ||
|
|
||
| The new `forwardRef` API solves this problem by providing a way for us to intercept a `ref` and forward it as a normal prop: | ||
| `embed:16-3-release-blog-post/forward-ref-example.js` | ||
|
|
||
| ## 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](/blog/2018/03/01/sneak-peek-beyond-react-16.html)) 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 [prototyping a React compiler](https://twitter.com/trueadm/status/944908776896978946). | ||
|
|
||
| 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**. | ||
| > | ||
| > Even in 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. | ||
|
|
||
| We are also adding a new static lifecycle, `getDerivedStateFromProps`, as a safer alternative to the legacy `componentWillReceiveProps`. | ||
|
|
||
| [Learn more about these lifecycle changes here.](#TODO) | ||
|
|
||
| ## `StrictMode` Component | ||
|
|
||
| `StrictMode` is a tool for highlighting potential problems in an application. Like `Fragment`, `StrictMode` does not render any visible UI. It 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. | ||
|
|
||
| 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.](/docs/strict-mode.html) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --- | ||
| id: forwarding-refs | ||
| title: Forwarding Refs | ||
| permalink: docs/forwarding-refs.html | ||
| --- | ||
|
|
||
| Ref forwarding is a technique for passing a [ref](/docs/refs-and-the-dom.html) through a component to one of its descendants. This technique can be particularly useful with [higher-order components](/docs/higher-order-components.html) (also known as HOCs). | ||
|
|
||
| Let's start with an example HOC that logs component props to the console: | ||
| `embed:forwarding-refs/log-props-before.js` | ||
|
|
||
| The "logProps" HOC passes all `props` through to the component it wraps, so the rendered output will be the same. For example, we can use this HOC to log all props that get passed to our "fancy button" component: | ||
| `embed:forwarding-refs/fancy-button.js` | ||
|
|
||
| There is one caveat to the above example: refs will not get passed through. That's because `ref` is not a prop. Like `key`, it's handled differently by React. If you add a ref to a HOC, the ref will refer to the outermost container component, not the wrapped component. | ||
|
|
||
| This means that refs intended for our `FancyButton` component will actually be attached to the `LogProps` component: | ||
| `embed:forwarding-refs/fancy-button-ref.js` | ||
|
|
||
| Fortunately, we can explicitly forward refs to the inner `FancyButton` component using the `React.forwardRef` API. `React.forwardRef` accepts a render function that receives `props` and `ref` parameters and returns a React node. For example: | ||
| `embed:forwarding-refs/log-props-after.js` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe “Strict Mode” as a title? It seems like this section uses concept names rather than API names for titles. Eg we have “forwarding refs” not “forwardRef”.