Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Usestate no actualiza el valor en el primer render

Estoy tratando de actualizar un estado de un objeto indefinido en Typescript.

Este es el estado que quiero actualizar:

 const [newEvents, setNewEvents] = useState<DataType[] | undefined>(undefined) type DataType = { Area: string; eventDate: string; eventId: string; type: string; geolocation: string; picUrls: string[]; }; useEffect(() => { _getNewEvents() }, []) async function _getNewEvents() { const result = await getEvents('hovedstaden'); var final_result = result as DataType[]; console.log(final_result) setNewEvents(final_result) console.log(newEvents) } return (...

La solicitud devuelve el siguiente objeto json:

 [{ Area: "hovedstaden", eventDate: "2022-05-05T21:00:00.000Z", eventId: "RasmusSeebachVega", geolocation: "55.67594,12.56553", picUrls: ["IMAGE_03","IMAGE_04"], type: "koncert", }, { Area: "hovedstaden", eventDate: "2022-05-15T20:00:00.000Z", eventId: "JonahBlacksmithVega", geolocation: "55.676098:12.568337", picUrls: ["IMAGE_01, IMAGE_02"], type: "koncert", }]

Y las salidas de console.log son las siguientes:

 Array [ Object { "Area": "hovedstaden", "eventDate": "2022-05-05T21:00:00.000Z", "eventId": "RasmusSeebachVega", "geolocation": "55.67594,12.56553", "picUrls": Array [ "IMAGE_03", "IMAGE_04", ], "type": "koncert", }, Object { "Area": "hovedstaden", "eventDate": "2022-05-15T20:00:00.000Z", "eventId": "JonahBlacksmithVega", "geolocation": "55.676098:12.568337", "picUrls": Array [ "IMAGE_01, IMAGE_02", ], "type": "koncert", }, ] undefined

La parte molesta es que el segundo archivo console.log no está definido, lo que significa que las dos últimas líneas de código no actualizan el estado. ¿Puede alguien señalar, lo que estoy haciendo mal?

Pregunta adicional: ¿Es necesario crear un tipo de datos para actualizar los valores de estado de los objetos json? Quiero decir, ¿y si no estuviera seguro de qué atributos contendría el objeto?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!