Skip to content

Commit 5778629

Browse files
committed
Doc updates for React 16 + blog post
1 parent 53548f7 commit 5778629

File tree

5 files changed

+254
-2
lines changed

5 files changed

+254
-2
lines changed

docs/_data/nav_docs.yml

Lines changed: 2 additions & 0 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

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
```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+
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

docs/docs/portals.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
id: portals
3+
title: Portals
4+
permalink: docs/portals.html
5+
---
6+
7+
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
8+
9+
```js
10+
ReactDOM.createPortal(
11+
child,
12+
container
13+
);
14+
```
15+
16+
The first argument (`child`) is any [renderable React child](/docs/react-component.html#render), such as an element, string, or fragment. The second argument (`container`) is a DOM element.
17+
18+
## Usage
19+
20+
Normally, when you return an element from a component's render method, it's mounted into the DOM as a child of the nearest parent node:
21+
22+
```js{4,6}
23+
render() {
24+
// React mounts a new div and renders the children into it
25+
return (
26+
<div>
27+
{this.props.children}
28+
</div>
29+
);
30+
}
31+
```
32+
33+
However, sometimes it's useful to insert a child into a different location in the DOM:
34+
35+
```js{6}
36+
render() {
37+
// React does *not* create a new div. It renders the children into `divNode`.
38+
// `divNode` is any valid DOM node, regardless of its location in the DOM.
39+
return React.createPortal(
40+
this.props.children,
41+
divNode,
42+
);
43+
}
44+
```
45+
46+
A typical use case for portals is when a parent component has an `overflow: hidden` or `z-index` style, but you need the child to visually "break out" of its container.
47+
48+
[Try out an example on CodePen.](https://codepen.io/acdlite/pen/JrKgmz)
49+
50+
## Portals and event bubbling
51+
52+
A nice feature of portals is that, even though the DOM node can be anywhere in the DOM tree, it behaves like a normal React child in every other way. Features like context work exactly the same regardless of whether the child is a portal.
53+
54+
This includes event bubbling: an event fired from inside a portal will propagate to ancestors in the containing *React tree*, even if those elements are not ancestors in the *DOM tree*:
55+
56+
```js
57+
// These two containers are siblings in the DOM
58+
const appContainer = document.getElementById('app-container');
59+
const modalContainer = document.getElementById('app-container');
60+
61+
class Parent extends React.Component {
62+
state = {clicks: 0};
63+
onClick = () => {
64+
// This will fire when the button in Child is clicked, even though
65+
// button is not direct descendant in the DOM.
66+
this.setState(state => ({clicks: state.clicks + 1}));
67+
};
68+
render() {
69+
return (
70+
<div onClick={this.onClick}>
71+
<p>Number of clicks: {this.state.clicks}</p>
72+
<p>Open up the browser DevTools to observe that the button is not a child the div with onClick handler.</p>
73+
{ReactDOM.createPortal(<Child />, modalContainer)}
74+
</div>
75+
);
76+
}
77+
}
78+
79+
function Child() {
80+
return <button>Click</button>;
81+
}
82+
83+
84+
ReactDOM.render(<Parent />, appContainer);
85+
```
86+
87+
[Try this example on CodePen](https://codepen.io/acdlite/pen/MEJEVV).
88+
89+
The advantage of treating event bubbling this way is that you can build abstractions around portals without the parent needing to know how it's implemented. For example, if your app uses a `<Modal />` component, you can switch its implementation to and from portals without having to change its parents to accomodate changes in event bubbling behavior.

docs/docs/reference-react-component.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,40 @@ render()
9191

9292
The `render()` method is required.
9393

94-
When called, it should examine `this.props` and `this.state` and return a single React element. This element can be either a representation of a native DOM component, such as `<div />`, or another composite component that you've defined yourself.
94+
When called, it should examine `this.props` and `this.state` and return one of the following types:
9595

96-
You can also return `null` or `false` to indicate that you don't want anything rendered. When returning `null` or `false`, `ReactDOM.findDOMNode(this)` will return `null`.
96+
- **React elements.** Typically created via JSX. A element can either be a representation of a native DOM component (`<div />`), or a user-defined composite component (`<MyComponent />`).
97+
- **String and numbers.** These are rendered as text nodes in the DOM.
98+
- **Portals**. Created with `React.createPortal`.
99+
- `null`. Renders nothing.
100+
- **Booleans**. Render nothing. (Mostly exists to support `return test && <Child />` pattern, where `test` is boolean.)
101+
102+
When returning `null` or `false`, `ReactDOM.findDOMNode(this)` will return `null`.
97103

98104
The `render()` function should be pure, meaning that it does not modify component state, it returns the same result each time it's invoked, and it does not directly interact with the browser. If you need to interact with the browser, perform your work in `componentDidMount()` or the other lifecycle methods instead. Keeping `render()` pure makes components easier to think about.
99105

100106
> Note
101107
>
102108
> `render()` will not be invoked if [`shouldComponentUpdate()`](#shouldcomponentupdate) returns false.
103109
110+
#### Fragments
111+
112+
You can also return multiple items from `render()` using an array:
113+
114+
```javascript
115+
render() {
116+
return [
117+
<li key="A"/>First item</li>,
118+
<li key="B"/>Second item</li>,
119+
<li key="C"/>Third item</li>,
120+
];
121+
}
122+
```
123+
124+
> Note:
125+
>
126+
> Don't forget to [add keys](https://facebook.github.io/react/docs/lists-and-keys.html#keys) to elements in a fragment to avoid the key warning.
127+
104128
* * *
105129

106130
### `constructor()`

www/src/theme.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ const sharedStyles = {
337337
'& li.button-newapp': {
338338
marginTop: 0,
339339
},
340+
341+
'& ol, & ul': {
342+
marginLeft: 20,
343+
},
340344
},
341345

342346
'& img': {

0 commit comments

Comments
 (0)