static getDerivedStateFromProps (props, state)
-
React 16.3
getDerivedStateFromProps is being added as a safer alternative to the legacy componentWillReceiveProps.
- static prefix is needed
- it will be called every time local state changes as well.
- this is useful when your props is async generated.
To make it only runs initially, you can do this:
static getDerivedStateFromProps(props, state) { if (props.opportunities !== state.opportunities && !state.opportunities.length) { return Object.assign(state, {opportunities: props.opportunities}) } return null; }