i’m learning reactJS and in this case i’m trying to assign passangers to a variable so i can later loop their name, ID..
I’m getting data from a formdata from a different file:
-Name
-Forename
-ID
I’m getting the data from “datos” variable so if in my for loop i’m getting datos.nombre in a correct way but cosole loging pasajero only logs the last person.
const { state } = useLocation();
const [datos] = useState(state);
const [pasajero, setPasajero] = useState({})
useEffect(() => {
for (let i = 0; i < datos.length; i++) {
setPasajero((prev) => ({
...prev,
nombre: datos.nombres[i],
apellido: datos.apellidos[i],
DNI: datos.DNI[i],
}));
}
},[]);
console.log(pasajero)
in my for loop i tried to assign the passanger but i’m getting the last value when i want to get every person in that for loop
Annie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.