When selecting an address from Places Autocomplete (ie onPress), I am calling a function to try and return the value of geocodeAsync from Expo location. I am however getting an invalid hook call when this function is being called, any advice would be lovely.
function getArray(data) {
React.useEffect(() => {
async function getPromise() {
const res = await geocodeAsync(data); // type: Promise<Interface>
setCoordinates(res);
}
void getPromise();
}, [])
const [coordinates2, setCoordinates] = React.useState<LocationGeocodedLocation[]>()
return coordinates2
}
Your outer function needs to have the prefix use so it can be parsed as a hook by React (i.e. useGetArray). You should also define your state before your useEffect so there's no risk of your set function being called before it's defined. See https://reactjs.org/docs/hooks-custom.html.
Also, consider turning on the React lint rules so you aren't missing dependencies in your useEffect.