⊗jsrtPmFmsSA 65 of 112 menu

Select items from an array in React

Let's say we have an array with the texts of the drop-down list items:

function App() { const texts = ['text1', 'text2', 'text3', 'text4']; const [value, setValue] = useState(''); return <div> ... </div>; }

Let's form our option tags in a loop:

function App() { const texts = ['text1', 'text2', 'text3', 'text4']; const [value, setValue] = useState(''); const options = texts.map((text, index) => { return <option key={index}>{text}</option>; }); return <div> <select value={value} onChange={(event) => setValue(event.target.value)}> {options} </select> <p> your choice: {value} </p> </div>; }

Let the array store a list of cities. Use a loop to output a drop-down list of these cities.

enru