Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 3 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # react-lifecycles-compat
  2. ## What is this project?
  3. React version 17 will deprecate several of the class component API lifecycles: `componentWillMount`, `componentWillReceiveProps`, and `componentWillUpdate`. (Read the [Update on Async rendering blog post](https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html) to learn more about why.) A couple of new lifecycles are also being added to better support [async rendering mode](https://reactjs.org/blog/2018/03/01/sneak-peek-beyond-react-16.html).
  4. Typically, this type of change would require third party libraries to release a new major version in order to adhere to semver. However, the `react-lifecycles-compat` polyfill offers a way to use the new lifecycles with older versions of React as well (0.14.9+) so no breaking release is required. This enables shared libraries to support both older and newer versions of React simultaneously.
  5. ## How can I use the polyfill
  6. First, install the polyfill from NPM:
  7. ```sh
  8. # Yarn
  9. yarn add react-lifecycles-compat
  10. # NPM
  11. npm install react-lifecycles-compat --save
  12. ```
  13. Next, update your component and replace any of the deprecated lifecycles with new ones introduced with React 16.3. (Refer to the React docs for [examples of how to use the new lifecycles](https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html).)
  14. Lastly, use the polyfill to make the new lifecycles work with older versions of React:
  15. ```js
  16. import React from 'react';
  17. import {polyfill} from 'react-lifecycles-compat';
  18. class ExampleComponent extends React.Component {
  19. static getDerivedStateFromProps(nextProps, prevState) {
  20. // Normally this method would only work for React 16.3 and newer,
  21. // But the polyfill will make it work for older versions also!
  22. }
  23. getSnapshotBeforeUpdate(prevProps, prevState) {
  24. // Normally this method would only work for React 16.3 and newer,
  25. // But the polyfill will make it work for older versions also!
  26. }
  27. // render() and other methods ...
  28. }
  29. // Polyfill your component so the new lifecycles will work with older versions of React:
  30. polyfill(ExampleComponent);
  31. export default ExampleComponent;
  32. ```
  33. ## Which lifecycles are supported?
  34. Currently, this polyfill supports [static `getDerivedStateFromProps`](https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops) and [`getSnapshotBeforeUpdate`](https://reactjs.org/docs/react-component.html#getsnapshotbeforeupdate)- both introduced in version 16.3.
  35. ## Validation
  36. Note that in order for the polyfill to work, none of the following lifecycles can be defined by your component: `componentWillMount`, `componentWillReceiveProps`, or `componentWillUpdate`.
  37. Note also that if your component contains `getSnapshotBeforeUpdate`, `componentDidUpdate` must be defined as well.
  38. An error will be thrown if any of the above conditions are not met.