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

111
Views
¿Cómo puedo hacer que una parte del código dentro de la función se ejecute después de las actualizaciones de estado o cuando no está indefinido en reaccionar nativo?

Estoy tratando de ejecutar un código en el bloque asíncrono después de las actualizaciones de estado, pero no estoy definido. Probé diferentes formas, como si el estado no fuera nulo, entonces esta parte del código se ejecuta pero no funciona.

Mi código:

 const fileMoveHandler = async () => { const fileName = selectedImage.split("/").pop(); const newPath = FileSystem.documentDirectory + fileName; setNewPathImage(newPath); //undefined try { await FileSystem.moveAsync({ from: selectedImage, to: newPath, }); } catch (err) { console.log("Error:", err); Alert.alert("Can't move this file.", "Try again!", [{ text: "Okay" }]); } }; const savePlaceHandler = () => { fileMoveHandler(); console.log("newPathImage: ", newPathImage); //here newPathImage is undefined dispatch(placesActions.addPlace(titleValue, newPathImage)); props.navigation.goBack(); };

Quiero ejecutar el método de dispatch después de las actualizaciones de estado.

¿Qué cambios debo hacer en mi código para que solo se ejecute si newPathImage no está indefinido?

¿Debo intentar promise o algo más?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar useEffect para ejecutar el envío cuando cambia newPath

 useEffect(() => { if(newPathImage){ dispatch(placesActions.addPlace(titleValue, newPathImage)); } }, [newPathImage]);

Si no lo quieres renderizar cada vez. Puede poner el método de envío en fileMoveHandler .

 const fileMoveHandler = async () => { const fileName = selectedImage.split("/").pop(); const newPath = FileSystem.documentDirectory + fileName; setNewPathImage(newPath); //undefined if(newPath){ dispatch(placesActions.addPlace(titleValue, newPath)); try { await FileSystem.moveAsync({ from: selectedImage, to: newPath, }); } catch (err) { console.log("Error:", err); Alert.alert("Can't move this file.", "Try again!", [{ text:"Okay" }]); } } };
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!