Answer the question
In order to leave comments, you need to log in
Removing an element from the UI in React. Do I need to pass the previous state to setState?
How would be more correct? So:
deleteItem = (id) => {
this.setState(({todoData}) => {
const idx = todoData.findIndex((el) => el.id === id);
const newArray = [
...todoData.slice(0, idx),
...todoData.slice(idx + 1)
]
return {
todoData: newArray
}
})
}
deleteItem = (id) => {
const todo = this.state.todoData.filter((todo) => {
return todo.id !== id;
});
this.setState({
todoData: todo,
});
};
Answer the question
In order to leave comments, you need to log in
newArray = [
...todoData.slice(0, idx),
...todoData.slice(idx + 1)
]
todoData.slice(0, idx).concat(todoData.slice(idx + 1))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question