I am using React to create a to do list app, in my app when I click x button to remove an Item and using console.log to check current array, I can see the array is updated correctly with the Item I want to remove removed from the list of array, but the Dom only renders the Item I want to remove instead of the whole array
import React from 'react';
import ReactDom from 'react-dom';
class TodoList extends React.Component{
constructor(props){
super(props);
this.state={
todoList:[],
todo:''
}
};
onValueChange=(e)=>{
const todo=e.target.value;
this.setState({todo})
};
removeItem=(props)=>{
this.setState({todoList:this.state.todoList.splice(props,1)})
console.log(this.state.todoList)
};
onSubmit=(e)=>{
e.preventDefault();
const {todoList,todo}=this.state
this.setState({todoList:[...todoList,todo]})
this.setState({todo:''})
console.log(this.state.todoList)
};
render(){
const myList=this.state.todoList.map((todo,index)=>(
<li key={index}>
{todo}
<button onClick={()=>this.removeItem(index)}>x</button>
</li>
))
return (
<div>
<form onSubmit={this.onSubmit}>
<input
type="text"
value={this.state.todo}
onChange={this.onValueChange}
autoFocus
placeholder='todo'
/>
</form>
<ol>
{myList}
</ol>
</div>
)
};
};
ReactDom.render(<TodoList/>,document.getElementById('app'));
this is the picture
in the picture you can see that the console shows the array with item '5' removed, but the screen only show the item '5' instead of the items 1 to 4