Skip to content

Commit 03cd206

Browse files
authored
Merge pull request #12 from bvaughn/static-lifecycle-methods-update
async-safe, static lifecycle hooks [update]
2 parents 4c613e8 + 7727fec commit 03cd206

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

text/0006-static-lifecycle-methods.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,12 @@ class ExampleComponent extends React.Component {
160160

161161
```js
162162
class ExampleComponent extends React.Component {
163+
// Initialize state in constructor,
164+
// Or with a property initializer.
165+
state = {};
166+
163167
static getDerivedStateFromProps(nextProps, prevState) {
164-
if (
165-
!prevState ||
166-
prevState.someMirroredValue !== nextProps.someValue
167-
) {
168+
if (prevState.someMirroredValue !== nextProps.someValue) {
168169
return {
169170
derivedData: computeDerivedState(nextProps),
170171
someMirroredValue: nextProps.someValue
@@ -359,33 +360,33 @@ class ExampleComponent extends React.Component {
359360

360361
## New static lifecycle methods
361362

362-
### `static getDerivedStateFromProps(nextProps: Props, prevState: State | null): PartialState | null`
363+
### `static getDerivedStateFromProps(nextProps: Props, prevState: State): PartialState | null`
363364

364-
This method is invoked after a component is constructed. Return an object to initialize component state. Note that the value of `prevState` may be null in this case if the constructor did not initialize `this.state`.
365+
This method is invoked after a component is instantiated and when it receives new props. Return an object to update state in response to prop changes. Return null to indicate no change to state.
365366

366-
This method is also invoked before a mounted component receives new props. Return an object to update state in response to prop changes. Return null to indicate no change to state.
367+
If an object is returned, its keys will be merged into the existing state.
367368

368369
Note that React may call this method even if the props have not changed. If calculating derived data is expensive, compare next and previous props to conditionally handle changes.
369370

370371
## Deprecated lifecycle methods
371372

372373
### `componentWillMount` -> `UNSAFE_componentWillMount`
373374

374-
This method will log a deprecation warning in development mode recommending that users use `componentDidMount` instead (when possible) or rename to `UNSAFE_componentWillMount`.
375+
This method is deprecated and will be removed in the next major version. Read about the motivations behind this change at [fb.me/react-async-component-lifecycle-hooks](https://fb.me/react-async-component-lifecycle-hooks)
375376

376-
`componentWillMount` will be removed entirely in version 17.
377+
As a temporary workaround, you can rename to `UNSAFE_componentWillMount` instead.
377378

378379
### `componentWillUpdate` -> `UNSAFE_componentWillUpdate`
379380

380-
This method will log a deprecation warning in development mode recommending that users use `componentDidUpdate` instead (when possible) or rename to `UNSAFE_componentWillUpdate`.
381+
This method is deprecated and will be removed in the next major version. Read about the motivations behind this change at [fb.me/react-async-component-lifecycle-hooks](https://fb.me/react-async-component-lifecycle-hooks)
381382

382-
`componentWillUpdate` will be removed entirely in version 17.
383+
As a temporary workaround, you can rename to `UNSAFE_componentWillUpdate` instead.
383384

384385
### `componentWillReceiveProps` -> `UNSAFE_componentWillReceiveProps`
385386

386-
This method will log a deprecation warning in development mode recommending that users use the new static `getDerivedStateFromProps` method instead (when possible) or rename to `UNSAFE_componentWillReceiveProps`.
387+
This method is deprecated and will be removed in the next major version. Use `static getDerivedStateFromProps()` instead. Read about the motivations behind this change at [fb.me/react-async-component-lifecycle-hooks](https://fb.me/react-async-component-lifecycle-hooks)
387388

388-
`componentWillReceiveProps` will be removed entirely in version 17.
389+
As a temporary workaround, you can rename to `UNSAFE_componentWillReceiveProps` instead.
389390

390391
# Drawbacks
391392

0 commit comments

Comments
 (0)