Skip to content

Commit 2d65e8f

Browse files
committed
Add Access The Latest Lifecycle Methods In An Old App as a react til
1 parent 8430b33 commit 2d65e8f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_660 TILs and counting..._
13+
_661 TILs and counting..._
1414

1515
---
1616

@@ -449,6 +449,7 @@ _660 TILs and counting..._
449449
### React
450450

451451
- [A Component Is Just A Bag Of Data](react/a-component-is-just-a-bag-of-data.md)
452+
- [Access The Latest Lifecycle Methods In An Old App](react/access-the-latest-lifecycle-methods-in-an-old-app.md)
452453
- [Accessing Env Vars In create-react-app](react/accessing-env-vars-in-create-react-app.md)
453454
- [Alter The Display Name Of A Component](react/alter-the-display-name-of-a-component.md)
454455
- [Building A React App In The Browser](react/building-a-react-app-in-the-browser.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Access The Latest Lifecycle Methods In An Old App
2+
3+
With the release of React 16.3 we have access to some new lifecycle methods
4+
and are in the first phase of what will eventually result in the deprecation
5+
and removal of `componentWillMount`, `componentWillReceiveProps`, and
6+
`componentWillUpdate`.
7+
8+
You may not be ready to move your project to React 16.3, but that doesn't
9+
mean you can't start migrating your lifecycle methods now. We'll need a
10+
polyfill --
11+
[react-lifecycles-compat](https://github.com/reactjs/react-lifecycles-compat).
12+
13+
```javascript
14+
import React from 'react';
15+
import { pollyfill } from 'react-lifecycles-compat';
16+
17+
class MyComponent extends React.Component {
18+
static getDerivedStateFromProps(nextProps, prevState) {
19+
// ...
20+
}
21+
22+
render() { ... }
23+
}
24+
polyfill(MyComponent)
25+
26+
export default MyComponent;
27+
```
28+
29+
For any of our class components for which we'd like to start using the new
30+
lifecycle methods, we just need to import the polyfill function and then
31+
transform the class component with the polyfill before exporting it.
32+
33+
[source](https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#open-source-project-maintainers)

0 commit comments

Comments
 (0)