【发布时间】:2018-12-08 02:31:29
【问题描述】:
示例:我想乘以从 API 获取的 2 个道具( Temp* 湿度)并希望在新值出现时更新结果(用户输入新城市然后温度和湿度改变)
这是我的 fetching.js
fetchData = q => {
fetch(
`http://api.openweathermap.org/data/2.5/weather?q=${q},th&units=metric&APPID=${API_KEY}`
)
.then(response => response.json())
.then(json => {
this.setState({
currentWeather: {
temp: json.main.temp,
icon: json.weather[0].icon,
description: json.weather[0].description,
humidity: json.main.humidity,
datetime: json.dt
}
});
})
.catch(error => {
console.warn(error);
});
};
这是我的前端
render() {
return (
<View style={styles.viewStyle}>
<View style={styles.dataStyle}>
<Text style={styles.textStyle}>Temperature </Text>
<Text style={styles.textStyle}>{this.props.temp} °C</Text>
</View>
<View style={styles.dataStyle}>
<Image
style={{ width: 100, height: 100 }}
source={{
uri: `http://openweathermap.org/img/w/${this.props.icon}.png`
}}
/>
<Text style={styles.textStyle}>{this.props.description}</Text>
</View>
<View style={styles.dataStyle}>
<Text style={styles.textStyle}>Humidity</Text>
<Text style={styles.textStyle}>{this.props.humidity} %</Text>
</View>
</View>
);
}
我想计算当温度和湿度传递到组件时的热指数并显示在湿度部分。
【问题讨论】:
-
你在尝试实现这一点时遇到了什么困难?
-
只需设置一个名为
heatIndex的新currentWeather属性并将其设置为json.main.temp * json.mainhumidy。为什么那行不通? -
抱歉,我是 javascript 新手。我只了解 state 和 props 的基本知识,但我不知道它能做什么。
标签: javascript reactjs react-native