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

231
Views
Reaccionar: no se puede actualizar el segmento de estado, pero otras propiedades de estado funcionan bien

Usando estas dos piezas de estado:

 this.markerArray = []; this.arrayLength = 0;

Estoy tratando de usar esta función:

 getMarkers = () => { this.dbRef.get().then((snapshot) => { snapshot.docs.forEach((marker) => { if (!this.markerArray.includes(marker.id)) { let currentID = marker.id; let appObj = { ...marker.data(), ["id"]: currentID, }; this.markerArray.push(appObj); } }); this.setState({ markerArray: this.markerArray, }); // (problem code) this.setState({arrayLength: this.markerArray.length}) return this.markerArray; }); };

Para actualizar una lista de marcadores de mapa. Estoy tratando de actualizar this.arrayLength cuando cambia la longitud de this.markerArray. Creo que el problema tiene que ver con la naturaleza asíncrona de setState, pero no sé qué hacer con esa información.

Yo he tratado:

  1. Creando una función separada para establecer arrayLength
  2. Pasar esa función como devolución de llamada al establecer el estado de markerArray
  3. Llamar a la función en Render (bucle infinito)
  4. Establecer arrayLength en el mismo setState que markerArray
  5. Cambiar el valor inicial de arrayLength a nulo, 0 y ''

Incluso intenté establecer arrayLength en un número arbitrario que no depende de markerArray.length, pero no se actualizará.

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!