Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React, { Component } from 'react';
import "../styles/MainPage.css"
class MainPage extends Component {
constructor(props) {
super(props);
this.state = {
currentWeather : [],
isLoaded: false,
error: null
}
}
componentDidMount(){
//token for API request
const token = "&APPID=dc7912235b38897f91307afe6e1162c3"
const unit = "&units=metric"
//API address
const apiUrl ='http://api.openweathermap.org/data/2.5/weather?q=Tampere,fi'
fetch(apiUrl+token+unit)
.then(res => res.json())
.then(
(result) => {
const tempArr =[]
tempArr.push(result.main.temp)
tempArr.push(result.wind.speed)
this.setState({
isLoaded: true,
currentWeather: tempArr
})
console.log(result)
},
(error) => {
this.setState({
isLoaded: true,
error
})
}
)
}
render() {
const {currentWeather, isLoaded} = this.state
if(isLoaded){
return (
<div className="weatherBox">
{/* <ul>
<li>testdata</li>
<li>testdata</li>
<li>testdata</li>
</ul>
*/}
<ul>
<li>Lämpötila: { JSON.stringify(currentWeather[0]) }C</li>
<li>Tuuli: { JSON.stringify(currentWeather[1]) }m/s</li>
<li>Tuntuu kuin: ?</li>
</ul>
</div>
);
}
else{
return <p>Lataa...</p>
}
}
}
export default MainPage;