Elementi select iz matrike v Reactu
Recimo, da imamo matriko z besedili elementov spustnega seznama:
function App() {
const texts = ['text1', 'text2', 'text3', 'text4'];
const [value, setValue] = useState('');
return <div>
...
</div>;
}
Ustvarimo naše oznake option v zanki:
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>
vaša izbira: {value}
</p>
</div>;
}
Recimo, da se v matriki shrani seznam mest. S pomočjo zanke prikažite spustni seznam teh mest.