我不知道如何从此功能中获取数据

时间:2019-11-11 19:44:32

标签: javascript reactjs function react-native

那么如何从那里获取变量info,以便可以在主屏幕中获取它提供的数据?

因此,代码的作用是打开应用程序时,console.log的信息看起来像this

它使用oneweathermap API和react本地地理位置。 我正在使用本机与expo。

import * as React from 'react';
import { Button, View, Text, TouchableOpacity, StyleSheet, setState} from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';

import axios from 'axios';

state = {
  location: null
};

  navigator.geolocation.getCurrentPosition(
    position => {
      const lat = position.coords.latitude;
      const lon = position.coords.longitude;
      //console.log(lon);
      //console.log(lat);

      const request_url = "https://api.openweathermap.org/data/2.5/weather?lat="+ lat +"&lon=" + lon + "&APPID=44c188ad06e81037a011c9486c76fc79";
      axios.get(request_url).then((response) => {
        if(response.status == 200){
          let info = response.data;
          console.log(info);
         }
      });

    },
    error => Alert.alert(error.message),
    { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
  );


class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
              <Text style={styles.welcome}>
        </Text>
      </View>
    );
  }
}

const RootStack = createStackNavigator(
  {
    Home: HomeScreen
  },
  {
    initialRouteName: 'Home',
  }
);

const AppContainer = createAppContainer(RootStack);

export default class App extends React.Component {
  render() {
    return <AppContainer />;
  }
}

0 个答案:

没有答案