Update for React 16.3 alpha introduced static getDerivedStateFromProps(nextProps, prevState)
(docs) as a replacement for componentWillReceiveProps
.
getDerivedStateFromProps is invoked after a component is instantiated as well as when it receives new props. It should return an object to update state, or null to indicate that the new props do not require any state updates.
Note that if a parent component causes your component to re-render, this method will be called even if props have not changed. You may want to compare new and previous values if you only want to handle changes.
https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops
It is static, therefore it does not have direct access to this
(however it does have access to prevState
, which could store things normally attached to this
e.g. refs
)
edited to reflect @nerfologist's correction in comments