I'm kinda new to React and i try to figure out how to change the state of my object's. But when i change the state in my input field the field is rerenderd at the bottom.
How can i keep my input field on the same place as it's renders the first time.
class App extends Component {
state = {
persons: [{
id: 0,
name: ''
},{
id: 1,
name: ''
}]
}
changePersonNameHandler = event => {
const id = parseInt(event.target.id);
const newPersonName = event.target.value;
this.setState({
persons: [...this.state.persons.filter(i => i.id !== id),
{
id,
name: newPersonName,
}
]
})
}
render () {
const {persons} = this.state;
return (
<div>
<ul>
{persons.map((i, index) => (
<li key={i.id}>
<label>Name: </label>
<input id={i.id}
value={i.name}
onChange{this.changePersonNameHandler}/>
</li>
)
)}
</ul>
</div>
)
}
}