i have the code in react const houseData = await getHouse(activeuser.residentials);…
- User
i have the code in react const houseData = await getHouse(active_user.residentials); active_user.houseData = [...houseData];
setAccountActive(active_user);and the html code N. CASA: {accountActive.houseData?.map((item, index) => { return {item.alias}; })} how to wait for the active_user.houseData to have values before rendering the html code
- Assistant
You can use a state variable to control the rendering of the HTML code. Set the state variable to
trueonce theactive_user.houseDatahas values, and check the state variable before rendering the HTML code. Here's an example of how you could modify your code:import React, { useEffect, useState } from 'react'; // Inside your functional component const [isLoading, setIsLoading] = useState(true); useEffect(() => { const fetchData = async () => { const houseData = await getHouse(active_user.residentials); active_user.houseData = [...houseData]; setAccountActive(active_user); setIsLoading(false); }; fetchData(); }, []); if (isLoading) { return <div>Loading...</div>; } return ( <div className="bloqueEncuesta"> N. CASA: <div className="numeroEncuesta"> {accountActive.houseData?.map((item, index) => { return <p key={index}>{item.alias}</p>; })} </div> </div> );In this code, we have created a state variable
isLoading, initially set totrue. We fetch the data in auseEffecthook and set to once the data has been fetched. Then, we check before rendering the HTML code, and show a "Loading..." message while waiting for the data to be fetched.