Skip to content

Instantly share code, notes, and snippets.

@kanhirun
Last active October 18, 2017 19:16
Show Gist options
  • Save kanhirun/5c0ce732d9cb17d07023baf3c815e39b to your computer and use it in GitHub Desktop.
Save kanhirun/5c0ce732d9cb17d07023baf3c815e39b to your computer and use it in GitHub Desktop.
mapStateToProps - strict filtering done by destructuring
// es6
const aStateThatCanBeSliced = {
fruits: {
oranges: 999999,
bananas: 10
},
hasFreshFruits: true,
}
const aStateThatCannotBeSliced = {
goats: 999
}
// Proposal
const mapStateToProps = state => {
const { fruits: { bananas } } = state;
return { fruits: { bananas } };
}
console.log(mapStateToProps(aStateThatCanBeSliced)) // => { fruits: { bananas: 10 } }
console.log(mapStateToProps(aStateThatCannotBeSliced)) // TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment