I have created a basic form:
On form submit, I'm converting the time from hours to minutes (if in minutes) but the problem is that state is not updating, as you can see Old Object and New Object.
This is the function in which I'm performing the update:
const onFormSubmit = (event) => {
event.preventDefault();
const updatedObject = {
name: newMovie.name,
ratings: newMovie.ratings,
duration: parseDuration(newMovie.duration),
};
console.log("Old object", newMovie);
setNewMovie(updatedObject);
console.log("New object", newMovie);
if (updatedObject.duration != 0) {
props.onNewMovieInput(newMovie);
}
};
This is the initial state:
const [newMovie, setNewMovie] = useState();
const [durationError, setDurationError] = useState(false);
const onDataUpdate = (event) => {
const key = event.target.id;
const value = event.target.value.trim();
setNewMovie((prevState) => {
return { ...prevState, [key]: value };
});
};
Where onDataUpdate is called on every onChange event.