Skip to content

Instantly share code, notes, and snippets.

@danieldunderfelt
Created May 26, 2018 05:03
Show Gist options
  • Save danieldunderfelt/8d3006a3f3ff446f88fe5ff7b3fb879e to your computer and use it in GitHub Desktop.
Save danieldunderfelt/8d3006a3f3ff446f88fe5ff7b3fb879e to your computer and use it in GitHub Desktop.
Updated initial state with getDerivedStateFromProps with React 16.4 compatibility.
import * as reduce from 'lodash/reduce'
import * as get from 'lodash/get'
import * as isMatch from 'lodash/isMatch'
export default (nextProps, state) => {
if (nextProps.mutationLoading) {
return null
}
const prevProps = get(state, '_prevProps', false)
if (prevProps && isMatch(nextProps, prevProps)) {
return null
}
const nextState = reduce(
state,
(returnState, value, prop) => {
if (typeof nextProps[prop] !== 'undefined') {
return { ...returnState, [prop]: nextProps[prop] }
}
return returnState
},
state
)
nextState._prevProps = nextProps
return nextState
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment