I have a conditional rendering while fetching user location using React Native and Expo. I am changing my state once the button is clicked to show spinner, however for some reason my state variable is always false. I understand useEffect hook however I don't think I need to change the variable on any particular lifecycle. Is this the right reasoning? At first I thought it was due to the async nature of the function however it does not seem likely.
State Init
const [loading, toggleLoading] = useState(false);
Code for onClick
{props.location === null ? (
<Button onPress={getLocationAsync} mode="contained">
Get Location
</Button>
) : loading === true ? (
<Button mode="contained">
<ActivityIndicator />
</Button>
) : (
<Button mode="outlined">Received</Button>
)}
Code for getLocationAsync
const getLocationAsync = async () => {
toggleLoading(true);
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== "granted") { }
let location = await Location.getCurrentPositionAsync();
toggleLoading(false);
};
Logging state anywhere always yields loading as false. Do I actually require something like useEffect in this case?