Skip to content

Instantly share code, notes, and snippets.

@Vincenius
Last active October 16, 2019 06:34

Revisions

  1. Vincenius revised this gist Oct 16, 2019. 1 changed file with 0 additions and 10 deletions.
    10 changes: 0 additions & 10 deletions toggleItemReact.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +0,0 @@
    onToggleArray = item => {
    this.setState(state => {
    const arr = state.arr.includes(item)
    ? arr.filter(i => i !== item) // remove item
    : [ ...arr, item ]; // add item
    return {
    arr,
    };
    });
    }
  2. Vincenius revised this gist Oct 16, 2019. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions toggleItemReact.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    onToggleArray = item => {
    this.setState(state => {
    const arr = state.arr.includes(item)
    ? arr.filter(i => i !== item) // remove item
    : [ ...arr, item ]; // add item
    return {
    arr,
    };
    });
    }
  3. Vincenius created this gist Oct 16, 2019.
    5 changes: 5 additions & 0 deletions toggleArrayItem.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    function toggleArrayItem(arr, item) {
    arr.includes(item)
    ? arr.filter(i => i !== item) // remove item
    : [ ...arr, item ]; // add item
    }