Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leonardovillela/515e701fd8f9e720c2a225357f97114d to your computer and use it in GitHub Desktop.
Save leonardovillela/515e701fd8f9e720c2a225357f97114d to your computer and use it in GitHub Desktop.
props-to-state-react.js
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