Looking for the best way to accomplish the following.
The goal is to, when this screen loads:
I'm also trying to useEffect so these only run once.
Function 1:
const getUserToken = async () => {
try {
const userToken = await AsyncStorage.getItem("userToken", userToken);
setUserToken(userToken);
} catch (err) {
console.log("err retrieving token " + err);
}
Function 2:
const getUserId = async () => {
try {
let response = await xano.get("/auth/me", {
headers: { Authorization: userToken },
});
setUserId(response.data.id);
console.log(userId);
} catch (err) {
console.log("getUserId err " + err);
console.log(err.data);
}
};
Function 3:
useEffect(() => {
getUserToken();
getUserId();
}, []);
You need to await those async functions in the useEffect hook.
useEffect(() => {
async function fetchMyAPI() {
await getUserToken();
await getUserId();
}
fetchMyAPI();
}, [])