Answer the question
In order to leave comments, you need to log in
Why item is not removed?
Good afternoon! Tell me why item is not removed, but added?
class App extends Component {
state = {
todoData:[
{ label: 'Drink Coffee', important: false, id: 1 },
{ label: 'Make Awesome App', important: true, id: 2 },
{ label: 'Have a lunch', important: false, id: 3 }
]
}
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
}
})
}
render(){
return (
<div className="todo-app">
<AppHeader toDo={1} done={3} />
<div className="top-panel d-flex">
<SearchPanel />
<ItemStatusFilter />
</div>
<TodoList todos={this.state.todoData} onDeleted={this.deleteItem} />
</div>
);
}
}
ReactDOM.render(<App />,
document.getElementById('root'));
Answer the question
In order to leave comments, you need to log in
why item is not removed, but added?
slice(0, -1)
slice(0)
deleteItem = id => {
this.setState(({ todoData }) => ({
todoData: todoData.filter(n => n.id !== id),
}));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question