【发布时间】:2018-10-05 20:54:56
【问题描述】:
我正在尝试让控制台使用 Open Weather Maps API 将输入城市和国家/地区的数据记录到带有 react-native 的 textInput 中。每当我在字段中输入一个城市和国家时,我都会得到“没有城市 404”。但是,如果我将一个硬编码到 const city 和 const country 的字符串所在的位置,我会得到我想要的回报。我不确定我做错了什么。我尝试了几种不同的方法并用谷歌搜索直到我的眼睛变红,但我被卡住了。我知道 API 并且一切正常,因为我能够对这些东西进行硬编码。这是我的 Form.JS 中的东西是我推测的。
感谢任何帮助。提前致谢!
这是我的 App.js 文件
//import a library to help create a component
import React, { Component } from 'react';
import { View, AppRegistry } from 'react-native';
//import components
import Header from './src/components/Header';
import Form from './src/components/Form';
import Weather from './src/components/Weather';
//API KEY from Open Weather Maps
const API_KEY = 'cad2d6dddccc9804f43e7c3af9e56f52';
const city = '';
const country = '';
export default class App extends Component {
getWeather = async (e) => {
e.preventDefault();
const api_call = await
fetch(`https://api.openweathermap.org/data/2.5/weather?
q=${city},${country}&appid=${API_KEY}&units=metric`);
const data = await api_call.json();
console.log(data);
}
render() {
return (
<View >
<Header headerText='Weather Fetcher' />
<Form
getWeather={this.getWeather}
city={this.city}
country={this.country}
/>
<Weather />
</View>
);
}
}
这是我的 Form.js 文件。
import React, { Component } from 'react';
import { TextInput, View, StyleSheet, Text, TouchableOpacity } from
'react-native';
export default class Form extends Component {
render() {
return (
<View style={styles.viewPut}>
<Text style={styles.textStyle}>City</Text>
<TextInput
style={styles.textInput}
value={this.city}
/>
<Text style={styles.textStyle}>Country</Text>
<TextInput
style={styles.textInput}
value={this.country}
/>
<TouchableOpacity
onPress={this.props.getWeather}
>
<Text> Fetch My Weather </Text>
</TouchableOpacity>
</View>
);
}
}
【问题讨论】:
标签: reactjs react-native react-props