Skip to content

Instantly share code, notes, and snippets.

@nelsondev19
Created August 13, 2021 04:38
Show Gist options
  • Save nelsondev19/2758706f7e2d56fd6372229150e074b9 to your computer and use it in GitHub Desktop.
Save nelsondev19/2758706f7e2d56fd6372229150e074b9 to your computer and use it in GitHub Desktop.
import { Fragment, useState } from "react";
function App() {
const [Autos] = useState(["volvo", "saab", "mercedes", "audi"]);
const [ValueSelected, setValueSelected] = useState(0);
const toggle = (AutoSeleccionado) => {
const indexSelected = Autos.findIndex((auto) => auto === AutoSeleccionado);
setValueSelected(indexSelected);
};
return (
<div>
<label for="cars">Choose a car:</label>
<select name="cars" id="cars" onChange={(e) => toggle(e.target.value)}>
{Autos.map((auto, index) => {
return (
<Fragment>
{index === ValueSelected ? (
<option className="active" value={auto}>
{auto}
</option>
) : (
<option value={auto}>{auto}</option>
)}
</Fragment>
);
})}
</select>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment