Tengo ganchos de reacción con estado de matriz. Me gustaría actualizar mis datos de fecha de este código.
const [[birthYear, birthMonth, birthDay], setStudentDob] = useState([]);
¿Cómo puedo cambiar con el evento OnChange en las reacciones?
//date
<select
value={birthDay}
onChange={handelDate}
name='day'
>
<option value='0'>Day</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
</select>
//birthMonth
<select
value={birthMonth}
onChange={handelDate}
name='month'
>
<option value='0'>Day</option>
<option value='01'>january</option>
<option value='02'>february</option>
<option value='03'>march</option>
</select>
</select>
//birthYear
<select
value={birthYear}
onChange={handelDate}
name='year'
>
<option value='0'>Day</option>
<option value='2020'>2020</option>
<option value='2021'>2021</option>
<option value='2020'>2022</option>
</select>
si tiene alguna forma preferible de actualizar, ayúdeme a encontrar estos problemas.
El handleDate puede ser así:
const handleDate = (value, index) => {
setStudentDob(s => {
let arr = [...s];
arr[index] = value;
return arr;
})
}
Asegúrese de pasar los parámetros correctos en el controlador onChange:
<select
value={birthDay}
onChange={e => handleDate(e.target.value, 2)}
name='day'
>
<select
value={birthMonth}
onChange={e => handleDate(e.target.value, 1)}
name='month'
>
<select
value={birthYear}
onChange={e => handleDate(e.target.value, 0)}
name='year'
>
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras