Skip to content

Commit acc8c47

Browse files
committed
Doc updates for React 16 + blog post
1 parent 4de45cf commit acc8c47

9 files changed

+461
-62
lines changed

docs/_data/nav_docs.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
title: Reconciliation
4747
- id: context
4848
title: Context
49+
- id: portals
50+
title: Portals
4951
- id: web-components
5052
title: Web Components
5153
- id: higher-order-components
@@ -65,8 +67,6 @@
6567
title: ReactDOM
6668
- id: react-dom-server
6769
title: ReactDOMServer
68-
- id: react-dom-node-stream
69-
title: ReactDOMNodeStream
7070
- id: dom-elements
7171
title: DOM Elements
7272
- id: events
@@ -77,3 +77,5 @@
7777
title: Shallow Renderer
7878
- id: test-renderer
7979
title: Test Renderer
80+
- id: javascript-environment-requirements
81+
title: JS Environment Requirements

docs/_posts/2017-09-26-react-v16.0.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
---
2+
title: "React v16.0"
3+
author: acdlite
4+
---
5+
6+
We're excited to announce the release of React v16.0! Among the changes are some long-standing feature requests, including [**fragments**](#new-render-return-types-fragments-and-strings), [**error boundaries**](#better-error-handling), [**portals**](#portals), support for [**custom DOM attributes**](#support-for-custom-dom-attributes), improved [**server-side rendering**](#better-server-side-rendering), and [**reduced file size**](#reduced-file-size).
7+
8+
### New render return types: fragments and strings
9+
10+
You can now return an array of elements from a component's `render` method. Like with other arrays, you'll need to add a key to each element to avoid the key warning:
11+
12+
```js
13+
render() {
14+
// No need to wrap list items in an extra element!
15+
return [
16+
// Don't forget the keys :)
17+
<li key="A">First item</li>,
18+
<li key="B">Second item</li>,
19+
<li key="C">Third item</li>,
20+
];
21+
}
22+
```
23+
24+
In the future, we'll likely add a special fragment syntax to JSX that doesn't require keys.
25+
26+
We've added support for returning strings, too:
27+
28+
```js
29+
render() {
30+
return 'Look ma, no spans!';
31+
}
32+
```
33+
34+
[See the full list of supported return types](/react/docs/react-component.html#render).
35+
36+
### Better error handling
37+
38+
Previously, runtime errors during rendering could put React in a broken state, producing cryptic error messages and requiring a page refresh to recover. To address this problem, React 16 uses a more resilient error-handling strategy. By default, if an error is thrown inside a component's render or lifecycle methods, the whole component tree is unmounted from the root. This prevents the display of corrupted data. However, it's probably not the ideal user experience.
39+
40+
Instead of unmounting the whole app every time there's an error, you can use error boundaries. Error boundaries are special components that capture errors inside their subtree and display a fallback UI in its place. Think of error boundaries like try-catch statements, but for React components.
41+
42+
For more details, check out our [previous post on error handling in React 16](/react/blog/2017/07/26/error-handling-in-react-16.html).
43+
44+
45+
### Portals
46+
47+
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
48+
49+
```js
50+
render() {
51+
// React does *not* create a new div. It renders the children into `domNode`.
52+
// `domNode` is any valid DOM node, regardless of its location in the DOM.
53+
return ReactDOM.createPortal(
54+
this.props.children,
55+
domNode,
56+
);
57+
}
58+
```
59+
60+
See a full example in the [documentation for portals](/react/docs/portals.html).
61+
62+
### Better server-side rendering
63+
64+
React 16 includes a completely rewritten server renderer. It's really fast. It supports **streaming**, so you can start sending bytes to the client faster. And thanks to a [new packaging strategy](#reduced-file-size) that compiles away `process.env` checks (Believe it or not, reading `process.env` in Node is really slow!), you no longer need to bundle React to get good server-rendering performance.
65+
66+
Core team member Sasha Aickin wrote a [great article describing React 16's SSR improvements](https://medium.com/@aickin/whats-new-with-server-side-rendering-in-react-16-9b0d78585d67). According to Sasha's synthetic benchmarks, server rendering in React 16 is roughly **three times faster** than React 15. "When comparing against React 15 with `process.env` compiled out, there’s about a 2.4x improvement in Node 4, about a 3x performance improvement in Node 6, and a full 3.8x improvement in the new Node 8.4 release. And if you compare against React 15 without compilation, React 16 has a full order of magnitude gain in SSR in the latest version of Node!" (As Sasha points out, please be aware that these numbers are based on synthetic benchmarks and may not reflect real-world performance.)
67+
68+
In addition, React 16 is better at hydrating server-rendered HTML once it reaches the client. It no longer requires the initial render to exactly match the result from the server. Instead, it will attempt to reuse as much of the existing DOM as possible. No more checksums! In general, we don't recommend that you render different content on the client versus the server, but it can be useful in some cases (e.g. timestamps).
69+
70+
See the [documentation for `ReactDOMServer`](/react/docs/react-dom-server.html) for more details.
71+
72+
### Support for custom DOM attributes
73+
74+
Instead of ignoring unrecognized HTML and SVG attributes, React will now [pass them through to the DOM](https://facebook.github.io/react/blog/2017/09/08/dom-attributes-in-react-16.html). This has the added benefit of allowing us to get rid of most of React's attribute whitelist, resulting in reduced file sizes.
75+
76+
### Reduced file size
77+
78+
Despite all these additions, React 16 is actually **smaller** compared to 15.6.1!
79+
80+
* `react` is 5.3 kb (2.2 kb gzipped), down from 20.7 kb (6.9 kb gzipped).
81+
* `react-dom` is 103.7 kb (32.6 kb gzipped), down from 141 kb (42.9 kb gzipped).
82+
* `react` + `react-dom` is 109 kb (34.8 kb gzipped), down from 161.7 kb (49.8 kb gzipped).
83+
84+
That amounts to a combined **32% size decrease compared to the previous version (30% post-gzip)**.
85+
86+
The size difference is partly attributable to a change in packaging. React now uses [Rollup](https://rollupjs.org/) to create flat bundles for each of its different target formats, resulting in both size and runtime performance wins. The flat bundle format also means that React's impact on bundle size is roughly consistent regardless of how your ship your app, whether it's with Webpack, Browserify, the pre-built UMD bundles, or any other system.
87+
88+
### MIT licensed
89+
90+
[In case you missed it](https://code.facebook.com/posts/300798627056246/relicensing-react-jest-flow-and-immutable-js/), React 16 is available under the MIT license. We've also published React 15.6.2 under MIT, for those who are unable to upgrade immediately.
91+
92+
### New core architecture
93+
94+
React 16 is the first version of React built on top of a new core architecture, codenamed "Fiber." You can read all about this project over on Facebook's engineering blog (TODO: link). (Spoiler: we rewrote React!)
95+
96+
Fiber is responsible for most of the new features in React 16, like error boundaries and fragments. Over the next few releases, you can expect more new features as we begin to unlock the full potential of React.
97+
98+
Perhaps the most exciting area we're working on is **async rendering**—a strategy for cooperatively scheduling rendering work by periodically yielding execution to the browser. The upshot is that, with async rendering, apps are more responsive because React avoids blocking the main thread.
99+
100+
This demo provides an early peek at the types of problems async rendering can solve:
101+
102+
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Ever wonder what &quot;async rendering&quot; means? Here&#39;s a demo of how to coordinate an async React tree with non-React work <a href="https://t.co/3snoahB3uV">https://t.co/3snoahB3uV</a> <a href="https://t.co/egQ988gBjR">pic.twitter.com/egQ988gBjR</a></p>&mdash; Andrew Clark (@acdlite) <a href="https://twitter.com/acdlite/status/909926793536094209">September 18, 2017</a></blockquote>
103+
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
104+
105+
*Tip: Pay attention to the spinning black square.*
106+
107+
We think async rendering is a big deal, and represents the future of React. To make migration to v16.0 as smooth as possible, we're not enabling any async features yet, but we're excited to start rolling them out in the coming months. Stay tuned!
108+
109+
## Installation
110+
111+
React v16.0.0 is available on the npm registry.
112+
113+
To install React 16 with Yarn, run:
114+
115+
```bash
116+
yarn add react@^16.0.0 react-dom@^16.0.0
117+
```
118+
119+
To install React 16 with npm, run:
120+
121+
```bash
122+
npm install --save react@^16.0.0 react-dom@^16.0.0
123+
```
124+
125+
We also provide UMD builds of React via a CDN:
126+
127+
```html
128+
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
129+
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
130+
```
131+
132+
Refer to the documentation for [detailed installation instructions](/react/docs/installation.html).
133+
134+
## Upgrading
135+
136+
Although React 16 includes significant internal changes, in terms of upgrading, you can think of this like any other major React release. We've been serving React 16 to Facebook and Messenger.com users since earlier this year, and we released several beta and release candidate versions to flush out additional issues. With minor exceptions, **if your app runs in 15.6 without any warnings, it should work in 16.**
137+
138+
### New deprecations
139+
140+
Hydrating a server-rendered container now has an explicit API. If you're reviving server-rendered HTML, use [`ReactDOM.hydrate`](/react/docs/react-dom.html#hydrate) instead of `ReactDOM.render`. Keep using `ReactDOM.render` if you're just doing client-side rendering.
141+
142+
### React Addons
143+
144+
As previously announced, we've [discontinued support for React Addons](/react/blog/2017/04/07/react-v15.5.0.html#discontinuing-support-for-react-addons). We expect the latest version of each addon (except `react-addons-perf`; see below) to work for the foreseeable future, but we won't publish additional updates.
145+
146+
Refer to the previous announcement for [suggestions on how to migrate](/react/blog/2017/04/07/react-v15.5.0.html#discontinuing-support-for-react-addons).
147+
148+
`react-addons-perf` no longer works at all in React 16. It's likely that we'll release a new version of this tool in the future. In the meantime, you can [use your browser's performance tools to profile React components](/react/docs/optimizing-performance.html#profiling-components-with-the-chrome-performance-tab).
149+
150+
### Breaking changes
151+
152+
React 16 includes a number of small breaking changes. These only affect uncommon use cases and we don't expect them to break most apps.
153+
154+
* React 15 had limited, undocumented support for error boundaries using `unstable_handleError`. This method has been renamed to `componentDidCatch`. You can use a codemod to [automatically migrate to the new API](https://github.com/reactjs/react-codemod#error-boundaries).
155+
* `ReactDOM.render` and `ReactDOM.unstable_renderIntoContainer` now return null if called from inside a lifecycle method. To work around this, you can use [portals](https://github.com/facebook/react/issues/10309#issuecomment-318433235) or [refs](https://github.com/facebook/react/issues/10309#issuecomment-318434635).
156+
* `setState`:
157+
* Calling `setState` with null no longer triggers an update. This allows you to decide in an updater function if you want to re-render.
158+
* Calling `setState` directly in render always causes an update. This was not previously the case. Regardless, you should not be calling setState from render.
159+
* `setState` callbacks (second argument) now fire immediately after `componentDidMount` / `componentDidUpdate` instead of after all components have rendered.
160+
* When replacing `<A />` with `<B />`, `B.componentWillMount` now always happens before `A.componentWillUnmount`. Previously, `A.componentWillUnmount` could fire first in some cases.
161+
* Previously, changing the ref to a component would always detach the ref before that component's render is called. Now, we change the ref later, when applying the changes to the DOM.
162+
* It is not safe to re-render into a container that was modified by something other than React. This worked previously in some cases but was never supported. We now emit a warning in this case. Instead you should clean up your component trees using `ReactDOM.unmountComponentAtNode`. [See this example.](https://github.com/facebook/react/issues/10294#issuecomment-318820987)
163+
* `componentDidUpdate` lifecycle no longer receives `prevContext` param. (See [#8631](https://github.com/facebook/react/issues/8631))
164+
* Shallow renderer no longer calls `componentDidUpdate` because DOM refs are not available. This also makes it consistent with `componentDidMount` (which does not get called in previous versions either).
165+
* Shallow renderer does not implement `unstable_batchedUpdates` anymore.
166+
167+
### Packaging
168+
169+
* There is no `react/lib/*` and `react-dom/lib/*` anymore. Even in CommonJS environments, React and ReactDOM are precompiled to single files (“flat bundles”). If you previously relied on undocumented React internals, and they don’t work anymore, let us know about your specific case in a new issue, and we’ll try to figure out a migration strategy for you.
170+
* There is no `react-with-addons.js` build anymore. All compatible addons are published separately on npm, and have single-file browser versions if you need them.
171+
* The deprecations introduced in 15.x have been removed from the core package. `React.createClass` is now available as `create-react-class`, `React.PropTypes` as `prop-types`, `React.DOM` as `react-dom-factories`, `react-addons-test-utils` as `react-dom/test-utils`, and shallow renderer as `react-test-renderer/shallow`. See [15.5.0](https://facebook.github.io/react/blog/2017/04/07/react-v15.5.0.html) and [15.6.0](https://facebook.github.io/react/blog/2017/06/13/react-v15.6.0.html) blog posts for instructions on migrating code and automated codemods.
172+
* The names and paths to the single-file browser builds have changed to emphasize the difference between development and production builds. For example:
173+
* `react/dist/react.js``react/umd/react.development.js`
174+
* `react/dist/react.min.js``react/umd/react.production.min.js`
175+
* `react-dom/dist/react-dom.js``react-dom/umd/react-dom.development.js`
176+
* `react-dom/dist/react-dom.min`.js → `react-dom/umd/react-dom.production.min.js`
177+
178+
## JavaScript Environment Requirements
179+
180+
React 16 depends on the collection types [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set). If you support older browsers and devices which may not yet provide these natively (e.g. IE < 11), consider including a global polyfill in your bundled application, such as [core-js](https://github.com/zloirock/core-js) or [babel-polyfill](https://babeljs.io/docs/usage/polyfill/).
181+
182+
A polyfilled environment for React 16 using core-js to support older browsers might look like:
183+
184+
```js
185+
import 'core-js/es6/map';
186+
import 'core-js/es6/set';
187+
188+
import React from 'react';
189+
import ReactDOM from 'react-dom';
190+
191+
ReactDOM.render(
192+
<h1>Hello, world!</h1>,
193+
document.getElementById('root')
194+
);
195+
```
196+
197+
React also depends on `requestAnimationFrame` (even in test environments). A simple shim for test environments would be:
198+
199+
```js
200+
global.requestAnimationFrame = function(callback) {
201+
setTimeout(callback, 0);
202+
};
203+
```
204+
205+
## Acknowledgments
206+
207+
As always, this release would not have been possible without our open source contributors. Thanks to everyone who filed bugs, opened PRs, responded to issues, wrote documentation, and more!
208+
209+
Special thanks to our core contributors, especially for their heroic efforts over the past few weeks during the prerelease cycle: [Brandon Dail](https://twitter.com/aweary), [Jason Quense](https://twitter.com/monasticpanic), [Nathan Hunzaker](https://twitter.com/natehunzaker), and [Sasha Aickin](https://twitter.com/xander76).

docs/docs/installation.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,21 @@ Learn [how to tell if your website is serving the right version of React](/react
215215

216216
### Using a CDN
217217

218-
If you don't want to use npm to manage client packages, the `react` and `react-dom` npm packages also provide single-file distributions in `dist` folders, which are hosted on a CDN:
218+
If you don't want to use npm to manage client packages, the `react` and `react-dom` npm packages also provide single-file distributions in `umd` folders, which are hosted on a CDN:
219219

220220
```html
221-
<script crossorigin src="https://unpkg.com/react@15/dist/react.js"></script>
222-
<script crossorigin src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
221+
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
222+
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
223223
```
224224

225225
The versions above are only meant for development, and are not suitable for production. Minified and optimized production versions of React are available at:
226226

227227
```html
228-
<script crossorigin src="https://unpkg.com/react@15/dist/react.min.js"></script>
229-
<script crossorigin src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
228+
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
229+
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
230230
```
231231

232-
To load a specific version of `react` and `react-dom`, replace `15` with the version number.
232+
To load a specific version of `react` and `react-dom`, replace `16` with the version number.
233233

234234
If you use Bower, React is available via the `react` package.
235235

0 commit comments

Comments
 (0)