==
created variables inputs and setinputs
in input value is varaible inputs.
on change call funciton
call handlechange
setInputs(values => Object.assign({}, values, { [name]: value }));
assigne the values to this object
=
=
ist mycar value loaded with volvo on page load normally it shows which one is ist value in drio down because of loaded value it showign as loaded value
function MyForm() {
const [myCar, setMyCar] = useState("Volvo");
const handleChange = (event) => {
setMyCar(event.target.value)
}
return (
<form>
<select value={myCar} onChange={handleChange}>
<option value="Ford">Ford</option>
<option value="Volvo">Volvo</option>
<option value="Fiat">Fiat</option>
</select>
</form>
)
}=
In this component:
Initial Value:
myCaris initialized with"Volvo"usinguseState("Volvo"), so"Volvo"is the default value for themyCarstate when the page first loads.
Select Element:
- The
<select>element has avalueprop set tomyCar. BecausemyCaris initialized as"Volvo", theselectdropdown will display"Volvo"as the selected option on page load.
- The
Changing the Selection:
- When the user selects a different option, the
handleChangefunction updatesmyCarto the selected option’svalue, and the dropdown updates accordingly.
- When the user selects a different option, the
So, even though "Ford" is the first option in the list, "Volvo" will display as selected initially because myCar was set to "Volvo" on load. This behavior is expected and correct.
=
No comments:
Post a Comment