Created
May 26, 2018 05:03
-
-
Save danieldunderfelt/8d3006a3f3ff446f88fe5ff7b3fb879e to your computer and use it in GitHub Desktop.
Updated initial state with getDerivedStateFromProps with React 16.4 compatibility.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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