In order to avoid .then(..)
I'm using await
. I'm calling a function which contains an Axios call, returned as response.data
after an await
. My functional component has a useEffect
which is supposed to set an initial variable based on the result of this function.
My error is: Unexpected reserved word 'await'
. It requires an async
, but where do I put that in my invocation of the function?
const fetchUserInfo = async () => {
const url = 'http://myapp.com/getUserInfo/';
const response = await axios.get(url);
return response.data;
}
function App() {
const [userInfo, setUserInfo] = useState({});
// On initialization, fetch & set UserInfo
useEffect(() => {
const result = await fetchUserInfo();
setUserInfo(result);
alert('Completed useEffect init');
}, []);
return (..);
}