Skip to content

Commit b843de7

Browse files
committed
Merge branch 'master' of https://github.com/reactjs/reactjs.org into defer-babel-loading
2 parents 7efe10f + 43e971f commit b843de7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+853
-522
lines changed

LICENSE-DOCS.md

Lines changed: 393 additions & 0 deletions
Large diffs are not rendered by default.

LICENSE.md

Lines changed: 21 additions & 393 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ This repo contains the source code and documentation powering [reactjs.org](http
88

99
1. Git
1010
1. Node: install version 8.4 or greater
11-
1. Yarn: `npm i -g yarn` to install it globally via NPM
12-
1. A clone of the [reactjs.org repo](https://github.com/facebook/reactjs.org) on your local machine
11+
1. Yarn: See [Yarn website for installation instructions](https://yarnpkg.com/lang/en/docs/install/)
12+
1. A clone of the [reactjs.org repo](https://github.com/reactjs/reactjs.org) on your local machine
1313
1. A fork of the repo (for any contributions)
1414

1515
### Installation
@@ -26,7 +26,7 @@ This repo contains the source code and documentation powering [reactjs.org](http
2626

2727
### Create a branch
2828

29-
1. `git checkout master` from any folder in your local react repository
29+
1. `git checkout master` from any folder in your local `reactjs.org` repository
3030
1. `git pull origin master` to ensure you have the latest main code
3131
1. `git checkout -b the-name-of-my-branch` (replacing `the-name-of-my-branch` with a suitable name) to create a branch
3232

@@ -41,16 +41,16 @@ This repo contains the source code and documentation powering [reactjs.org](http
4141
### Test the change
4242

4343
1. If possible, test any visual changes in all latest versions of common browsers, on both desktop and mobile.
44-
1. Run `yarn check-all` from the project root. (This will run Prettier, ESlint, and Flow.)
44+
1. Run `yarn check-all` from the project root. (This will run Prettier, ESLint, and Flow.)
4545

4646
### Push it
4747

4848
1. `git add -A && git commit -m "My message"` (replacing `My message` with a commit message, such as `Fixed header logo on Android`) to stage and commit your changes
4949
1. `git push my-fork-name the-name-of-my-branch`
50-
1. Go to the [reactjs.org repo](https://github.com/facebook/reactjs.org) and you should see recently pushed branches.
50+
1. Go to the [reactjs.org repo](https://github.com/reactjs/reactjs.org) and you should see recently pushed branches.
5151
1. Follow GitHub's instructions.
5252
1. If possible include screenshots of visual changes. A Netlify build will also be automatically created once you make your PR so other people can see your change.
5353

5454
## Troubleshooting
5555

56-
- `yarn reset` to clear the local cache
56+
- `yarn reset` to clear the local cache

content/blog/2015-01-27-react-v0.13.0-beta-1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ Therefore we decided not to have this built-in into React's class model. You can
8989

9090
```javascript
9191
class Counter extends React.Component {
92-
constructor() {
93-
super();
92+
constructor(props) {
93+
super(props);
9494
this.tick = this.tick.bind(this);
9595
}
9696
tick() {

content/blog/2017-07-26-error-handling-in-react-16.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ We also encourage you to use JS error reporting services (or build your own) so
7979

8080
React 16 prints all errors that occurred during rendering to the console in development, even if the application accidentally swallows them. In addition to the error message and the JavaScript stack, it also provides component stack traces. Now you can see where exactly in the component tree the failure has happened:
8181

82-
<img src="../images/blog/error-boundaries-stack-trace.png" alt="Component stack traces in error message" style="width: 100%;">
82+
<img src="../images/docs/error-boundaries-stack-trace.png" alt="Component stack traces in error message" style="width: 100%;">
8383

8484
You can also see the filenames and line numbers in the component stack trace. This works by default in [Create React App](https://github.com/facebookincubator/create-react-app) projects:
8585

86-
<img src="../images/blog/error-boundaries-stack-trace-line-numbers.png" alt="Component stack traces with line numbers in error message" style="width: 100%;">
86+
<img src="../images/docs/error-boundaries-stack-trace-line-numbers.png" alt="Component stack traces with line numbers in error message" style="width: 100%;">
8787

8888
If you don’t use Create React App, you can add [this plugin](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source) manually to your Babel configuration. Note that it’s intended only for development and **must be disabled in production**.
8989

content/docs/codebase-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The [fbjs repository](https://github.com/facebook/fbjs) exists because React sha
6666
After cloning the [React repository](https://github.com/facebook/react), you will see a few top-level folders in it:
6767

6868
* [`src`](https://github.com/facebook/react/tree/master/src) is the source code of React. **If your change is related to the code, `src` is where you'll spend most of your time.**
69-
* [`docs`](https://github.com/facebook/react/tree/master/docs) is the React documentation website. When you change APIs, make sure to update the relevant Markdown files.
69+
* [`docs`](https://github.com/reactjs/reactjs.org/tree/master/content) is the React documentation website. When you change APIs, make sure to update the relevant Markdown files.
7070
* [`fixtures`](https://github.com/facebook/react/tree/master/fixtures) contains a few small React test applications for contributors.
7171
* [`packages`](https://github.com/facebook/react/tree/master/packages) contains metadata (such as `package.json`) for all packages in the React repository. Nevertheless, their source code is still located inside [`src`](https://github.com/facebook/react/tree/master/src).
7272
* `build` is the build output of React. It is not in the repository but it will appear in your React clone after you [build it](/docs/how-to-contribute.html#development-workflow) for the first time.

content/docs/error-boundaries.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
id: error-boundaries
3+
title: Error Boundaries
4+
permalink: docs/error-boundaries.html
5+
---
6+
7+
In the past, JavaScript errors inside components used to corrupt React’s internal state and cause it to [emit](https://github.com/facebook/react/issues/4026) [cryptic](https://github.com/facebook/react/issues/6895) [errors](https://github.com/facebook/react/issues/8579) on next renders. These errors were always caused by an earlier error in the application code, but React did not provide a way to handle them gracefully in components, and could not recover from them.
8+
9+
10+
## Introducing Error Boundaries
11+
12+
A JavaScript error in a part of the UI shouldn’t break the whole app. To solve this problem for React users, React 16 introduces a new concept of an “error boundary”.
13+
14+
Error boundaries are React components that **catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI** instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
15+
16+
A class component becomes an error boundary if it defines a new lifecycle method called `componentDidCatch(error, info)`:
17+
18+
```js{7-12,15-18}
19+
class ErrorBoundary extends React.Component {
20+
constructor(props) {
21+
super(props);
22+
this.state = { hasError: false };
23+
}
24+
25+
componentDidCatch(error, info) {
26+
// Display fallback UI
27+
this.setState({ hasError: true });
28+
// You can also log the error to an error reporting service
29+
logErrorToMyService(error, info);
30+
}
31+
32+
render() {
33+
if (this.state.hasError) {
34+
// You can render any custom fallback UI
35+
return <h1>Something went wrong.</h1>;
36+
}
37+
return this.props.children;
38+
}
39+
}
40+
```
41+
42+
Then you can use it as a regular component:
43+
44+
```js
45+
<ErrorBoundary>
46+
<MyWidget />
47+
</ErrorBoundary>
48+
```
49+
50+
The `componentDidCatch()` method works like a JavaScript `catch {}` block, but for components. Only class components can be error boundaries. In practice, most of the time you’ll want to declare an error boundary component once and use it throughout your application.
51+
52+
Note that **error boundaries only catch errors in the components below them in the tree**. An error boundary can’t catch an error within itself. If an error boundary fails trying to render the error message, the error will propagate to the closest error boundary above it. This, too, is similar to how catch {} block works in JavaScript.
53+
54+
### componentDidCatch Parameters
55+
56+
`error` is an error that has been thrown.
57+
58+
`info` is an object with `componentStack` key. The property has information about component stack during thrown error.
59+
60+
```js
61+
//...
62+
componentDidCatch(error, info) {
63+
64+
/* Example stack information:
65+
in ComponentThatThrows (created by App)
66+
in ErrorBoundary (created by App)
67+
in div (created by App)
68+
in App
69+
*/
70+
logComponentStackToMyService(info.componentStack);
71+
}
72+
73+
//...
74+
```
75+
76+
## Live Demo
77+
78+
Check out [this example of declaring and using an error boundary](https://codepen.io/gaearon/pen/wqvxGa?editors=0010) with [React 16 beta](https://github.com/facebook/react/issues/10294).
79+
80+
81+
## Where to Place Error Boundaries
82+
83+
The granularity of error boundaries is up to you. You may wrap top-level route components to display a “Something went wrong” message to the user, just like server-side frameworks often handle crashes. You may also wrap individual widgets in an error boundary to protect them from crashing the rest of the application.
84+
85+
86+
## New Behavior for Uncaught Errors
87+
88+
This change has an important implication. **As of React 16, errors that were not caught by any error boundary will result in unmounting of the whole React component tree.**
89+
90+
We debated this decision, but in our experience it is worse to leave corrupted UI in place than to completely remove it. For example, in a product like Messenger leaving the broken UI visible could lead to somebody sending a message to the wrong person. Similarly, it is worse for a payments app to display a wrong amount than to render nothing.
91+
92+
This change means that as you migrate to React 16, you will likely uncover existing crashes in your application that have been unnoticed before. Adding error boundaries lets you provide better user experience when something goes wrong.
93+
94+
For example, Facebook Messenger wraps content of the sidebar, the info panel, the conversation log, and the message input into separate error boundaries. If some component in one of these UI areas crashes, the rest of them remain interactive.
95+
96+
We also encourage you to use JS error reporting services (or build your own) so that you can learn about unhandled exceptions as they happen in production, and fix them.
97+
98+
99+
## Component Stack Traces
100+
101+
React 16 prints all errors that occurred during rendering to the console in development, even if the application accidentally swallows them. In addition to the error message and the JavaScript stack, it also provides component stack traces. Now you can see where exactly in the component tree the failure has happened:
102+
103+
<img src="../images/docs/error-boundaries-stack-trace.png" style="max-width:100%" alt="Error caught by Error Boundary component">
104+
105+
You can also see the filenames and line numbers in the component stack trace. This works by default in [Create React App](https://github.com/facebookincubator/create-react-app) projects:
106+
107+
<img src="../images/docs/error-boundaries-stack-trace-line-numbers.png" style="max-width:100%" alt="Error caught by Error Boundary component with line numbers">
108+
109+
If you don’t use Create React App, you can add [this plugin](https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source) manually to your Babel configuration. Note that it’s intended only for development and **must be disabled in production**.
110+
111+
112+
## Why Not Use try/catch?
113+
114+
`try` / `catch` is great but it only works for imperative code:
115+
116+
```js
117+
try {
118+
showButton();
119+
} catch (error) {
120+
// ...
121+
}
122+
```
123+
124+
However, React components are declarative and specify *what* should be rendered:
125+
126+
```js
127+
<Button />
128+
```
129+
130+
Error boundaries preserve the declarative nature of React, and behave as you would expect. For example, even if an error occurs in a `componentDidUpdate` hook caused by a `setState` somewhere deep in the tree, it will still correctly propagate to the closest error boundary.
131+
132+
133+
## Naming Changes from React 15
134+
135+
React 15 included a very limited support for error boundaries under a different method name: `unstable_handleError`. This method no longer works, and you will need to change it to `componentDidCatch` in your code starting from the first 16 beta release.
136+
137+
For this change, we’ve provided a [codemod](https://github.com/reactjs/react-codemod#error-boundaries) to automatically migrate your code.

content/docs/forms.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,21 @@ this.setState(partialState);
264264

265265
Also, since `setState()` automatically [merges a partial state into the current state](/docs/state-and-lifecycle.html#state-updates-are-merged), we only needed to call it with the changed parts.
266266

267+
## Controlled Input Null Value
268+
269+
Specifying the value prop on a [controlled component](/docs/forms.html#controlled-components) prevents the user from changing the input unless you desire so. If you've specified a `value` but the input is still editable, you may have accidentally set `value` to `undefined` or `null`.
270+
271+
The following code demonstrates this. (The input is locked at first but becomes editable after a short delay.)
272+
273+
```javascript
274+
ReactDOM.render(<input value="hi" />, mountNode);
275+
276+
setTimeout(function() {
277+
ReactDOM.render(<input value={null} />, mountNode);
278+
}, 1000);
279+
280+
```
281+
267282
## Alternatives to Controlled Components
268283

269284
It can sometimes be tedious to use controlled components, because you need to write an event handler for every way your data can change and pipe all of the input state through a React component. This can become particularly annoying when you are converting a preexisting codebase to React, or integrating a React application with a non-React library. In these situations, you might want to check out [uncontrolled components](/docs/uncontrolled-components.html), an alternative technique for implementing input forms.

content/docs/higher-order-components.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ For example, say you have a `CommentList` component that subscribes to an extern
3030

3131
```js
3232
class CommentList extends React.Component {
33-
constructor() {
34-
super();
33+
constructor(props) {
34+
super(props);
3535
this.handleChange = this.handleChange.bind(this);
3636
this.state = {
3737
// "DataSource" is some global data source

content/docs/installation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ Here are a couple of ways to get started:
2424

2525
If you're just interested in playing around with React, you can use CodePen. Try starting from [this Hello World example code](http://codepen.io/gaearon/pen/rrpgNB?editors=0010). You don't need to install anything; you can just modify the code and see if it works.
2626

27-
If you prefer to use your own text editor, you can also <a href="https://raw.githubusercontent.com/facebook/react/master/docs/downloads/single-file-example.html" download="hello.html">download this HTML file</a>, edit it, and open it from the local filesystem in your browser. It does a slow runtime code transformation, so don't use it in production.
27+
If you prefer to use your own text editor, you can also <a href="https://raw.githubusercontent.com/reactjs/reactjs.org/master/static/html/single-file-example.html" download="hello.html">download this HTML file</a>, edit it, and open it from the local filesystem in your browser. It does a slow runtime code transformation, so don't use it in production.
2828

2929
If you want to use it for a full application, there are two popular ways to get started with React: using Create React App, or adding it to an existing application.
3030

3131
## Creating a New Application
3232

33-
[Create React App](http://github.com/facebookincubator/create-react-app) is the best way to start building a new React single page application. It sets up your development environment so that you can use the latest JavaScript features, provides a nice developer experience, and optimizes your app for production.
33+
[Create React App](http://github.com/facebookincubator/create-react-app) is the best way to start building a new React single page application. It sets up your development environment so that you can use the latest JavaScript features, provides a nice developer experience, and optimizes your app for production. You’ll need to have Node >= 6 on your machine.
3434

3535
```bash
3636
npm install -g create-react-app
@@ -84,7 +84,7 @@ Both Yarn and npm download packages from the [npm registry](http://npmjs.com/).
8484

8585
We recommend using React with [Babel](http://babeljs.io/) to let you use ES6 and JSX in your JavaScript code. ES6 is a set of modern JavaScript features that make development easier, and JSX is an extension to the JavaScript language that works nicely with React.
8686

87-
The [Babel setup instructions](https://babeljs.io/docs/setup/) explain how to configure Babel in many different build environments. Make sure you install [`babel-preset-react`](http://babeljs.io/docs/plugins/preset-react/#basic-setup-with-the-cli-) and [`babel-preset-es2015`](http://babeljs.io/docs/plugins/preset-es2015/#basic-setup-with-the-cli-) and enable them in your [`.babelrc` configuration](http://babeljs.io/docs/usage/babelrc/), and you're good to go.
87+
The [Babel setup instructions](https://babeljs.io/docs/setup/) explain how to configure Babel in many different build environments. Make sure you install [`babel-preset-react`](http://babeljs.io/docs/plugins/preset-react/#basic-setup-with-the-cli-) and [`babel-preset-env`](http://babeljs.io/docs/plugins/preset-env/) and enable them in your [`.babelrc` configuration](http://babeljs.io/docs/usage/babelrc/), and you're good to go.
8888

8989
### Hello World with ES6 and JSX
9090

content/docs/nav.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
title: Context
4949
- id: portals
5050
title: Portals
51+
- id: error-boundaries
52+
title: Error Boundaries
5153
- id: web-components
5254
title: Web Components
5355
- id: higher-order-components

content/docs/reference-react-component.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ componentWillUpdate(nextProps, nextState)
216216

217217
`componentWillUpdate()` is invoked immediately before rendering when new props or state are being received. Use this as an opportunity to perform preparation before an update occurs. This method is not called for the initial render.
218218

219-
Note that you cannot call `this.setState()` here. If you need to update state in response to a prop change, use `componentWillReceiveProps()` instead.
219+
Note that you cannot call `this.setState()` here; nor should you do anything else (eg dispatch a redux action) that would trigger an update to a React component before `componentWillUpdate` returns. Use `componentWillReceiveProps()` if you need to update `state` in response to `props` changes.
220220

221221
> Note
222222
>

0 commit comments

Comments
 (0)