I'm using React Functional Components. I'm familiar with the class lifecycle, but not with the functional lifecycle. My problem is that this component is rendered before the initialization performed in useEffect().. is complete. In my return() I check whether a state variable is true or not.
- I see my non-Error layout first (
TopNav&Main) - In 2-3 sec. the screen changes to the
Errorlayout
What is the order in which functional components are processed? I can't present then non-Error layout until the error condition has been evaluated.
function App() {
const [error, setError] = useState({});
const [userInfo, setUserInfo] = useState({});
const fetchUserInfo = async () => {
const url = 'http://myapp.com/fetchUserInfo';
try {
const response = await axios.get(url);
setUserInfo(response.data);
}
catch (error) {
setError(error);
}
}
// On initialization, fetch UserInfo
useEffect(() => {
fetchUserInfo();
}, []);
return (
<div className="App">
<Router>
<Header />
{error.message
?
<Error error={error} />
:
<div>
<TopNav userInfo={userInfo} />
<Main userInfo={userInfo} />
</div>
}
<Footer />
</Router>
</div>
);
}