and two components:
function ExamenAplicado({alternativas}) {
const [alternativas, setAlternativas] = useState([{pregunta: 1, alternativa: 0},{pregunta: 2, alternativa: 0},{pregunta: 3, alternativa: 0},{pregunta: 4, alternativa: 0},{pregunta: 5, alternativa: 0}]);
const handleChange = (e) => {
setAlternativas({
...alternativas,
[e.target.name] : e.target.value
})
}
return (
<div className="container my-5">
<div className="row">
{alternativas.map(x => ( <ExamenRespuesta key={x.pregunta} numeroPregunta={x.pregunta} handleChange={handleChange} /> ))}
</div>
</div>
)
}
function ExamenRespuesta({numeroPregunta, handleChange}) {
return(
<div>
pregunta {numeroPregunta}:
<select id={numeroPregunta} name={numeroPregunta} onChange={handleChange} className="form-control">
<option value="0">vacio</option>
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="4">d</option>
<option value="5">e</option>
</select>
</div>
)
}
I don't know how to bind the to each element of the array. So when you change a value of the select, change the value of the "alternativa" field.
The idea is that the pregunta value of the array is linked to the name property of the select and the alternativa value is linked to the value property of the select.
pregunta ~~ [select control].name
alternativa ~~ [select control].value
Please help. I need an idea of how to achieve this functionality.