Skip to content

Instantly share code, notes, and snippets.

@pi0
Forked from farzadso/No mutations at all
Last active June 28, 2018 08:23
Show Gist options
  • Save pi0/3f23d5803556de68ce996927bbfaba93 to your computer and use it in GitHub Desktop.
Save pi0/3f23d5803556de68ce996927bbfaba93 to your computer and use it in GitHub Desktop.
This is what I call clean coding
import assert from 'assert';
updateStepFourProps(type, operation, id) {
assert(Array.isArray(this.state[type]), `Cannot do operation on type '${type}'`)
switch (operation) {
case 'add':
this.setState({
[type]: this.state[type].concat(id)
})
break;
case 'remove':
this.setState({
[type]: this.state[type].filter(item => item.id !== id)
})
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment