|
| 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: |
| 11 | + |
| 12 | +```javascript |
| 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 | +Support for returning strings from the render method has also been added. |
| 25 | + |
| 26 | +[API Documentation](/docs/react-component.html#render) |
| 27 | + |
| 28 | +### Better error handling |
| 29 | + |
| 30 | + |
| 31 | +Previously, runtime errors during rendering could potentially 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. |
| 32 | + |
| 33 | +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. |
| 34 | + |
| 35 | +For more details, check out our [previous post on error handling in React 16](/blog/2017/07/26/error-handling-in-react-16.html). |
| 36 | + |
| 37 | + |
| 38 | +### Portals |
| 39 | + |
| 40 | +Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. |
| 41 | + |
| 42 | +```js{6} |
| 43 | +render() { |
| 44 | + // React does *not* create a new div. It renders the children into `divNode`. |
| 45 | + // `divNode` is any valid DOM node, regardless of its location in the DOM. |
| 46 | + return React.createPortal( |
| 47 | + this.props.children, |
| 48 | + divNode, |
| 49 | + ); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +See a full example in the [documentation for portals](/docs/portals.html) |
| 54 | + |
| 55 | +### Better server-side rendering |
| 56 | + |
| 57 | +React 16 is better at hydrating server-rendered HTML. 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! |
| 58 | + |
| 59 | +The server renderer has also been completely rewritten to support streaming. React core team member Sasha Aicken, who worked on this feature, wrote a [great article describing React 16's SSR improvements](https://medium.com/@aickin/whats-new-with-server-side-rendering-in-react-16-9b0d78585d67). "Rendering to a stream can reduce the time to first byte (TTFB) for your content, sending the beginning of the document down the wire to the browser before the next part of the document has even been generated. All major browsers will start parsing and rendering the document earlier when the content is streamed from the server this way." |
| 60 | + |
| 61 | +### Support for custom DOM attributes |
| 62 | + |
| 63 | +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. |
| 64 | + |
| 65 | +### Reduced file size |
| 66 | + |
| 67 | +Despite all these additions, React 16 is actually **smaller** compared to 15.6.1! |
| 68 | + |
| 69 | +* `react` is 5.3 kb (2.2 kb gzipped), down from 20.7 kb (6.9 kb gzipped). |
| 70 | +* `react-dom` is 103.7 kb (32.6 kb gzipped), down from 141 kb (42.9 kb gzipped). |
| 71 | +* `react` + `react-dom` is 109 kb (34.8 kb gzipped), down from 161.7 kb (49.8 kb gzipped). |
| 72 | + |
| 73 | +That amounts to a combined **32% size decrease compared to the previous version (30% post-gzip)**. |
| 74 | + |
| 75 | +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 |
| 76 | + |
| 77 | +### New core architecture |
| 78 | + |
| 79 | +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!) |
| 80 | + |
| 81 | +Most of the features in this release, like error boundaries and fragments, were made possible by the rewritten core. Over the next few releases, you can expect more new features as we begin to unlock the full potential of React. |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +See this demo for an early peek at the types of problems async rendering can solve. TODO: link to demo. |
| 86 | + |
| 87 | +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 roll this out in the coming months. Stay tuned! |
| 88 | + |
| 89 | +## Upgrading |
| 90 | + |
| 91 | +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.** |
| 92 | + |
| 93 | +Deprecations |
| 94 | + |
| 95 | +Hydrating a server-rendered container now has an explicit API. Use ReactDOM.hydrate instead of ReactDOM.render if you're reviving server-rendered HTML. Keep using ReactDOM.render if you're just doing client-side rendering. |
| 96 | + |
| 97 | +Documentation (TODO: link) |
| 98 | + |
| 99 | +Breaking changes |
| 100 | + |
| 101 | +* 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). |
| 102 | +* 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). |
| 103 | +* `setState`: |
| 104 | + * Calling `setState` with null no longer triggers an update. This allows you to decide in an updater function if you want to re-render. |
| 105 | + * Calling `setState` directly in render always causes an update. This was not previously the case. Regardless, you should not be calling setState from render. |
| 106 | + * `setState` callbacks (second argument) now fire immediately after `componentDidMount` / `componentDidUpdate` instead of after all components have rendered. |
| 107 | +* When replacing `<A />` with `<B />`, `B.componentWillMount` now always happens before `A.componentWillUnmount`. Previously, `A.componentWillUnmount` could fire first in some cases. |
| 108 | +* 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. |
| 109 | +* 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) |
| 110 | +* `componentDidUpdate` lifecycle no longer receives `prevContext` param. (See #8631) |
| 111 | +* 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). |
| 112 | +* Shallow renderer does not implement `unstable_batchedUpdates()` anymore. |
| 113 | + |
| 114 | +Packaging |
| 115 | + |
| 116 | +* 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 this issue, and we’ll try to figure out a migration strategy for you. |
| 117 | +* 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. |
| 118 | +* 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. |
| 119 | +* The names and paths to the single-file browser builds have changed to emphasize the difference between development and production builds. For example: |
| 120 | + * `react/dist/react.js` → `react/umd/react.development.js` |
| 121 | + * `react/dist/react.min.js` → `react/umd/react.production.min.js` |
| 122 | + * `react-dom/dist/react-dom.js` → `react-dom/umd/react-dom.development.js` |
| 123 | + * `react-dom/dist/react-dom.min`.js → `react-dom/umd/react-dom.production.min.js` |
| 124 | + |
| 125 | +JavaScript environment requirements |
| 126 | + |
| 127 | +TODO: inline JavaScript environment requirements doc |
| 128 | + |
| 129 | +Acknowledgments |
| 130 | + |
| 131 | +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! |
| 132 | + |
| 133 | +Special thanks to the following core team members: TODO |
0 commit comments