From 95f2a184d0d786e7548e4a8cf8ae269382368c73 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Wed, 28 Mar 2018 10:08:14 -0700 Subject: [PATCH] Added a note about mirroring props in state and no prevProps on gDSFP --- content/blog/2018-03-27-update-on-async-rendering.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/content/blog/2018-03-27-update-on-async-rendering.md b/content/blog/2018-03-27-update-on-async-rendering.md index ed50b6fdd..3f06db7e3 100644 --- a/content/blog/2018-03-27-update-on-async-rendering.md +++ b/content/blog/2018-03-27-update-on-async-rendering.md @@ -128,6 +128,12 @@ Although the above code is not problematic in itself, the `componentWillReceiveP As of version 16.3, the recommended way to update `state` in response to `props` changes is with the new `static getDerivedStateFromProps` lifecycle. (That lifecycle is called when a component is created and each time it receives new props): `embed:update-on-async-rendering/updating-state-from-props-after.js` +You may notice in the example above that `props.currentRow` is mirrored in state (as `state.lastRow`). This enables `getDerivedStateFromProps` to access the previous props value in the same way as is done in `componentWillReceiveProps`. + +You may wonder why we don't just pass previous props as a parameter to `getDerivedStateFromProps`. We considered this option when designing the API, but ultimately decided against it for two reasons: +* A `prevProps` parameter would be null the first time `getDerivedStateFromProps` was called (after instantiation), requiring an if-not-null check to be added any time `prevProps` was accessed. +* Not passing the previous props to this function is a step toward freeing up memory in future versions of React. (If React does not need to pass previous props to lifecycles, then it does not need to keep the previous `props` object in memory.) + > Note > > If you're writing a shared component, the [`react-lifecycles-compat`](https://github.com/reactjs/react-lifecycles-compat) polyfill enables the new `getDerivedStateFromProps` lifecycle to be used with older versions of React as well. [Learn more about how to use it below.](#open-source-project-maintainers)