Paolo Guerra
Paolo Guerra

Reputation: 259

Is it possible to pass custom argument to setState callback function?

Is there a way to pass argument to setState(function(prevState, props, custom_rgument)?

removeTodo(evt) {
    this.setState((prevState, props, evt) => ({
        todoList: [...prevState.todoList.slice(0, evt), ...prevState.todoList.slice(evt + 1)]
    }))
}

Or the only way to achieve this is to work with state?

removeTodo() {
    this.setState(prevState => ({
        todoList: [...prevState.todoList.slice(0, this.state.index), ...prevState.todoList.slice(this.state.index + 1)]
    }))
}

Upvotes: 0

Views: 1071

Answers (1)

Fabian Schultz
Fabian Schultz

Reputation: 18546

There shouldn't be a need to pass the evt parameter to the setState function. It's available in the scope anyways.

removeTodo(evt) {
  this.setState(prevState => ({
    todoList: [
      ...prevState.todoList.slice(0, evt),
      ...prevState.todoList.slice(evt + 1)
    ]
  }));
}

Upvotes: 1

Related Questions