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

151
Views
reaccionar useState nativo no está incrementando el contador

Cuando el contador es >= data SetLoading de longitud de matriz debería activarse. Pero el contador no se incrementa hasta que actualizo la aplicación presionando CTRL+S y luego el contador se incrementa en 1 cada vez que la aplicación se actualiza.

 const [loopCount, setLoopCount] = useState('1'); const [isLoading, setLoading] = useState(true); useEffect(() => { const getPackages = async () => { try { const offerings = await Purchases.getOfferings(); if (offerings.current !== null) { var counter = 0; data.forEach((category, index) => { //data is array with length of 6 if(category.entitlement_id){ AsyncStorage.getItem(category.entitlement_id).then((value) => { console.log("Loop COUNT before is : "+loopCount); setLoopCount(loopCount+1); //This should increment on every iteration of forEach console.log("Loop COUNT after is : "+loopCount); }); }else{ console.log("ELSE COUNTER" + counter); setLoopCount(loopCount+1); } if(loopCount >= data.length){ //if counter is >= data length do something setLoading(false); } }) }else{ console.log("No offerings found"); } } catch (e) { console.log("Error => " + e); } } getPackages(); }, [])
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

La actualización del estado es una operación asincrónica, por lo que obtener loopCount en for loop obtendrá el valor inicial de loopCount en lugar del valor actualizado. Si no necesita actualizar la interfaz de usuario para actualizar loopCount , puede usar useRef en lugar de useState :

 const loopCount = useRef(1); const [isLoading, setLoading] = useState(true); useEffect(() => { const getPackages = async () => { try { const offerings = await Purchases.getOfferings(); if (offerings.current !== null) { var counter = 0; data.forEach((category, index) => { //data is array with length of 6 if(category.entitlement_id){ AsyncStorage.getItem(category.entitlement_id).then((value) => { console.log("Loop COUNT before is : "+loopCount.current); loopCount.current = loopCount.current+1; //This should increment on every iteration of forEach console.log("Loop COUNT after is : "+loopCount.current); }); }else{ console.log("ELSE COUNTER" + counter); loopCount.current = loopCount.current+1; } if(loopCount.current >= data.length){ //if counter is >= data length do something setLoading(false); } }) }else{ console.log("No offerings found"); } } catch (e) { console.log("Error => " + e); } } getPackages(); }, [])
about 4 years ago · Juan Pablo Isaza Report

0

Dudo que el contador sea realmente necesario aquí, sugeriría una solución con AsyncStorage.getItem síncrono

 const [isLoading, setLoading] = useState(true); useEffect(() => { const getPackages = async () => { try { const offerings = await Purchases.getOfferings(); if (offerings.current !== null) { var counter = 0; await Promise.all( data.map(async (category, index) => { if (category.entitlement_id) { await AsyncStorage.getItem(category.entitlement_id).then(value => { }); } else { console.log('ELSE COUNTER' + counter); } }) ); setLoading(false); } else { console.log('No offerings found'); } } catch (e) { console.log('Error => ' + e); } }; getPackages(); }, []);
about 4 years ago · Juan Pablo Isaza Report
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!