Skip to content

Instantly share code, notes, and snippets.

@gpDA
Last active May 1, 2019 21:50
Show Gist options
  • Save gpDA/3859fb22a4ed2b59e72386a550ba761e to your computer and use it in GitHub Desktop.
Save gpDA/3859fb22a4ed2b59e72386a550ba761e to your computer and use it in GitHub Desktop.
// ./components.App.js
// decrement function
handleIncrement = counter => {
// copy the `counters`
const counters = [...this.state.counters];
// indexOf corresponding eventActions
const index = counters.indexOf(counter);
// update the `counters` Object using new `counter`
counters[index] = {...counter};
counters[index].value++;
// setState at last
this.setState({ counters });
}
// increment function
handleDecrement = counter => {
// copy the `counters`
const counters = [...this.state.counters];
// indexOf corresponding eventActions
const index = counters.indexOf(counter);
// update the `counters` Object using new `counter`
counters[index] = {...counter};
counters[index].value--;
// setState at last
this.setState({ counters });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment