Skip to content

Commit 24cf457

Browse files
authored
Tweak Lifecycle Docs (reactjs#199)
* Tweak Lifecycle Docs * Nits
1 parent b30cdf6 commit 24cf457

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

content/docs/reference-react-component.md

+22-10
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,13 @@ constructor(props)
141141

142142
The constructor for a React component is called before it is mounted. When implementing the constructor for a `React.Component` subclass, you should call `super(props)` before any other statement. Otherwise, `this.props` will be undefined in the constructor, which can lead to bugs.
143143

144-
The constructor is the right place to initialize state. If you don't initialize state and you don't bind methods, you don't need to implement a constructor for your React component.
144+
Avoid introducing any side-effects or subscriptions in the constructor. For those use cases, use `componentDidMount()` instead.
145145

146-
It's okay to initialize state based on props. This effectively "forks" the props and sets the state with the initial props. Here's an example of a valid `React.Component` subclass constructor:
146+
The constructor is the right place to initialize state. To do so, just assign an object to `this.state`; don't try to call `setState()` from the constructor. The constructor is also often used to bind event handlers to the class instance.
147+
148+
If you don't initialize state and you don't bind methods, you don't need to implement a constructor for your React component.
149+
150+
In rare cases, it's okay to initialize state based on props. This effectively "forks" the props and sets the state with the initial props. Here's an example of a valid `React.Component` subclass constructor:
147151

148152
```js
149153
constructor(props) {
@@ -154,7 +158,7 @@ constructor(props) {
154158
}
155159
```
156160

157-
Beware of this pattern, as state won't be up-to-date with any props update. Instead of syncing props to state, you often want to [lift the state up](/docs/lifting-state-up.html).
161+
Beware of this pattern, as state won't be up-to-date with any props update. Instead of syncing props to state, you often want to [lift the state up](/docs/lifting-state-up.html) instead.
158162

159163
If you "fork" props by using them for state, you might also want to implement [`componentWillReceiveProps(nextProps)`](#componentwillreceiveprops) to keep the state up-to-date with them. But lifting state up is often easier and less bug-prone.
160164

@@ -166,9 +170,11 @@ If you "fork" props by using them for state, you might also want to implement [`
166170
componentWillMount()
167171
```
168172

169-
`componentWillMount()` is invoked immediately before mounting occurs. It is called before `render()`, therefore setting state synchronously in this method will not trigger a re-rendering. Avoid introducing any side-effects or subscriptions in this method.
173+
`componentWillMount()` is invoked immediately before mounting occurs. It is called before `render()`, therefore calling `setState()` synchronously in this method will not trigger an extra rendering. Generally, we recommend using the `constructor()` instead.
174+
175+
Avoid introducing any side-effects or subscriptions in this method. For those use cases, use `componentDidMount()` instead.
170176

171-
This is the only lifecycle hook called on server rendering. Generally, we recommend using the `constructor()` instead.
177+
This is the only lifecycle hook called on server rendering.
172178

173179
* * *
174180

@@ -178,7 +184,11 @@ This is the only lifecycle hook called on server rendering. Generally, we recomm
178184
componentDidMount()
179185
```
180186

181-
`componentDidMount()` is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. Setting state in this method will trigger a re-rendering.
187+
`componentDidMount()` is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request.
188+
189+
This method is a good place to set up any subscriptions. If you do that, don't forget to unsubscribe in `componentWillUnmount()`.
190+
191+
Calling `setState()` in this method will trigger an extra rendering, but it is guaranteed to flush during the same tick. This guarantees that even though the `render()` will be called twice in this case, the user won't see the intermediate state. Use this pattern with caution because it often causes performance issues. It can, however, be necessary for cases like modals and tooltips when you need to measure a DOM node before rendering something that depends on its size or position.
182192

183193
* * *
184194

@@ -192,7 +202,7 @@ componentWillReceiveProps(nextProps)
192202

193203
Note that React may call this method even if the props have not changed, so make sure to compare the current and next values if you only want to handle changes. This may occur when the parent component causes your component to re-render.
194204

195-
React doesn't call `componentWillReceiveProps` with initial props during [mounting](#mounting). It only calls this method if some of component's props may update. Calling `this.setState` generally doesn't trigger `componentWillReceiveProps`.
205+
React doesn't call `componentWillReceiveProps()` with initial props during [mounting](#mounting). It only calls this method if some of component's props may update. Calling `this.setState()` generally doesn't trigger `componentWillReceiveProps()`.
196206

197207
* * *
198208

@@ -224,7 +234,9 @@ componentWillUpdate(nextProps, nextState)
224234

225235
`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.
226236

227-
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.
237+
Note that you cannot call `this.setState()` here; nor should you do anything else (e.g. dispatch a Redux action) that would trigger an update to a React component before `componentWillUpdate()` returns.
238+
239+
If you need to update `state` in response to `props` changes, use `componentWillReceiveProps()` instead.
228240

229241
> Note
230242
>
@@ -254,7 +266,7 @@ Use this as an opportunity to operate on the DOM when the component has been upd
254266
componentWillUnmount()
255267
```
256268

257-
`componentWillUnmount()` is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any DOM elements that were created in `componentDidMount`
269+
`componentWillUnmount()` is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in `componentDidMount()`.
258270

259271
* * *
260272

@@ -266,7 +278,7 @@ componentDidCatch(error, info)
266278

267279
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.
268280

269-
A class component becomes an error boundary if it defines this lifecycle method.
281+
A class component becomes an error boundary if it defines this lifecycle method. Calling `setState()` in it lets you capture an unhandled JavaScript error in the below tree and display a fallback UI. Only use error boundaries for recovering from unexpected exceptions; don't try to use them for control flow.
270282

271283
For more details, see [*Error Handling in React 16*](/blog/2017/07/26/error-handling-in-react-16.html).
272284

0 commit comments

Comments
 (0)