Can anyone explaine me please, just start learn React, I’m confused in one thing in this code:
import { useState } from “react”;
import ReactDOM from “react-dom/client”;
function Car() {
const [car, setCar] = useState({
brand: "Ford",
model: "Mustang",
year: "1964",
color: "red"
});
const updateColor = () => {
setCar(previousState => {
return { ...previousState, color: "blue" }
});
}
return (
<>
<h1>My {car.brand}</h1>
<p>
It is a {car.color} {car.model} from {car.year}.
</p>
<button
type="button"
onClick={updateColor}
>Blue</button>
</>
)
}
const root = ReactDOM.createRoot(document.getElementById(‘root’));
root.render();
previousState , If we only called setCar({color: “blue”}), this would remove the brand, model, and year from our state,
so previousState concatenating them, but how previousState accessed brand, model and year? ewhen they were attributed to him?
previousState is not a keyword no?
nick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.