Created
August 13, 2021 04:38
-
-
Save nelsondev19/2758706f7e2d56fd6372229150e074b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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