Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to call current temperature from openweathermap with axios

$
0
0

I am trying to do an axios call to get the latest temperature using openweatherapi. However for some reason I cant find out why my axios call is not working. Any help would be great, here is my code:

   function Overview() {    const appState = useRef(AppState.currentState);    const [count, setCount] = useState(0);    const [active, setActive] = useState(false);    const [latitude, setLatitude] = useState(0);    const [longitude, setLongitude] = useState(0);    const [data, setData] = useState();    const getLocationAsync = async () => {        let {status} = await Location.requestForegroundPermissionsAsync();        if (status !== 'granted') {            console.log('Location Permission Error')        }        const loc = await Location.getCurrentPositionAsync({});        setLatitude(loc.coords.latitude);        setLongitude(loc.coords.longitude);    }    useEffect(() => {        appState.current.match('active') ? setActive(true) : setActive(false)        active && setInterval(() => setCount((oldCount) => oldCount + 1), 1000);        getLocationAsync();        return () => {            clearInterval();        };    }, [active]);   const getWeather = async () => {       const req = axios.get(`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${api_key}`);       const res = await req;       setData(res.data.main.temp)    };   // useEffect(() =>  {getWeather()}, [])    return (<View style={{margin: 32}}><TouchableOpacity onPress={getWeather}><Text>{data ? data : 'no data'}</Text></TouchableOpacity></View>    );}

Any advice on what im doing wrong would be great!


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>