I'm not sure what's going wrong. I am importing a function that uses useState and useEffect. the function returns the variable in my state with data from a fetch call. However, when I try to map over that data, I get an error: map is not defined. My syntax looks right and this works in regular react, so I'm not sure what's going wrong now. I am getting a response back, the file is being imported/exported correctly and the state is updating (the APIKey used is hidden for this post) and pointers would be great!
code for data and updating state:
export function grabLocationData() {
const [data, updateData] = useState([]);
console.log("Data!", data);
const [location, updateLocation] = useState("");
useEffect(() => {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log("lat", position.coords.latitude);
console.log("lng", position.coords.longitude);
const { latitude, longitude } = position.coords;
updateLocation(`${latitude},${longitude}`);
},
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
const url = `https://maps.googleapis.com/maps/api/place/textsearch/json?query=${query}&location=${location}&radius=10000&key=${apiKey}`;
console.log("console log!!!", url);
fetch(url)
.then((response, err) => {
if (err) throw err;
return response.json();
})
.then((responseJson) => {
updateData(responseJson);
});
}, []);
return data;
}
code for mapping
export default Locations = (props) => {
const apiKey = "AIzaSyD3o3hDRwSZTVhlUIDOjGQ1ZqevG6fnWII";
const data = grabLocationData();
console.log(data);
return (
<ScrollView>
{data.results.map((bank) => {
return <View>{bank.name}</View>;
})}
</ScrollView>
);
};
updateLocationin your effect. But it is still empty when you finish executing the hook. Therefore, the location part of the url will be empty when you try to do the fetch. Try splitting the code into two effects. One that sets the location. The second that fetches the json data once location is defined. Also remember to use the correct dependencies for your useEffect. React hooks docs are great place to refresh.