0

I have created a list in states, When I want to add a some thing to list, I check its length to show a button or not and it works but when I want to remove something from list, if statement doesn't work to remove button.

this.state = {
  allTasks: []
}

This is my addToList function :

addToList() {
    if (this.state.inputValue === '') {
      this.setState({ invalidInput: 'ورودی شما خالی است، لطفا چک کنید !' });
      return;
    }
    this.setState({
      allTasks: [...this.state.allTasks, this.state.inputValue],
      inputValue: ''
    });

    if (this.state.allTasks.length > 0) {
      document.getElementById('removeAllTasksButton').style.display = 'block';
    } else {
      document.getElementById('removeAllTasksButton').style.display = 'none';
    }
}

This is removeFromList function :

removeFromList(x) {
  this.setState({
    allTasks: this.state.allTasks.filter((e, i) => i !== x)
  });

  if (this.state.allTasks.length < 1) {
    document.getElementById('removeAllTasksButton').style.display = 'none';
  } else {
    document.getElementById('removeAllTasksButton').style.display = 'block';
  }
}
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142

0 Answers0