I am a beginner in React and I follow a tutorial in Udemy.I had a confusion about state. When I am trying to update the state depending on the previous state, why the mentor says that we need to always use the second approach while both approaches seems logical to me.
This is my initialization
const [UserInput, setUserInput] = useState({
enteredTitle:'',
enteredDate:'',
enteredAmount:''
});
So here is the first approach.
const handleTitleChange = (event) =>{
setUserInput({
...UserInput,
enteredTitle:event.target.value
})
}
This is my second approach.
const handleTitleChange = (event) =>{
setUserInput((prevState) => {
return{
...prevState, enteredTitle:event.target.value}
});