I have the following functions in react const reload = async(index) => {…
- User
I have the following functions in react const reload = async(index) => { setNomenclaturaFull([]); setSelectOption([]); getHouses(nomenclaturas); setSelectOption(nomenclaturaFull[index]); console.log("reloaded"); };
const getHouses = (nomenclaturas) => { let houses = nomenclaturas.data.map(async (item, index) => { let data = { ...item }; let houses = await getNomenclaturasCasasHelper( "Yd5MV7jkA0MjQeCmzKrZ", nomenclaturas.data[index].id ).then((houseData) => { data.houseData = houseData; }); return data; }); Promise.all(houses).then((data) => { setNomenclaturaFull(data); }); };useEffect(() => { if (selectOption) { let profile = selectOption.houseData.map(async (item, index) => { let data = { ...item }; if (data.idOwners) { const profile = await getProfileByHouseIdHelper( data.idOwners[0] ).then((pro) => { data.userProfile = pro; }); } return data; }); Promise.all(profile).then((data) => { setSelectHouseDataFull(data); }); } }, [selectOption]);
setSelectedOption is setting up the older version of the variable nomenclaturas, I want it to wait for getHouses to set the new version of nomenclaturas
- Assistant
To solve this issue, you can use a state variable to know if the
nomenclaturaFullstate has been updated after the execution of thegetHousesfunction. You can then use this flag in theuseEffectforselectOption.Here's what you need to do:
- Create a new state variable to track the status of
nomenclaturaFullupdate:
const [isNomenclaturaFullUpdated, setIsNomenclaturaFullUpdated] = useState(false); - Create a new state variable to track the status of