Sign in
Log inSign up

How to setState inside of a render function

jim toby's photo
jim toby
·Oct 16, 2018

I have an array in my state that contains strings. Basically I want to be able to delete values from the array after users have completed an action. Here is my code to delete from the array:

removeColumn(index) {
    this.setState(prevState => ({
          arrayOfColumns: this.state.arrayOfColumns.filter((_, i) => i !== index)
        }));
      }
}

Let's say the element in the array I want to delete is "1". Inside of my render function, I call it like this:

this.removeColumn.bind(this, this.state.arrayOfColumns.indexOf("1"));
//And than I print the array
console.log(this.state.arrayOfColumns)

WHen I print the array, it prints all the elements as though nothing has been deleted. Can anyone help me with this? Any help is greatly appreciated.

Thanks!