Created
January 18, 2017 13:15
-
-
Save leonardovillela/515e701fd8f9e720c2a225357f97114d to your computer and use it in GitHub Desktop.
props-to-state-react.js
This file contains 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
componentWillReceiveProps(props) { | |
const propsToCheck = [ | |
{prop: 'data', state: 'dataRows', path: 'data.dataRows'}, | |
{prop: 'count', state: 'count', path: 'data.dataCount'}, | |
{prop: 'selectedRowIds'} | |
]; | |
const nextState = _.chain(propsToCheck) | |
.filter(it => this.props[it.prop] !== props[it.prop]) | |
.map(it => ([it.state || it.prop, _.get(props, it.path || it.prop)])) | |
.fromPairs() | |
.value(); | |
this.setState(nextState); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment