mirror of
https://github.com/bendtherules/buildit-weather-app.git
synced 2026-08-18 13:42:14 +00:00
Load data into state
This commit is contained in:
@@ -40,8 +40,17 @@ export interface Forecast5Days {
|
|||||||
list: Array<ForecaseAtInstance>;
|
list: Array<ForecaseAtInstance>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WeatherApp extends React.Component {
|
export interface WeatherDataState {
|
||||||
data: Forecast5Days | undefined = undefined;
|
data: Forecast5Days | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WeatherApp extends React.Component<{}, WeatherDataState> {
|
||||||
|
|
||||||
|
constructor(props: {}) {
|
||||||
|
super(props);
|
||||||
|
|
||||||
|
this.state = { data: undefined };
|
||||||
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
fetch('https://api.openweathermap.org/data/2.5/forecast?q=kolkata&appid=d65cee3c5866168080fa1c4177fdb4a8')
|
fetch('https://api.openweathermap.org/data/2.5/forecast?q=kolkata&appid=d65cee3c5866168080fa1c4177fdb4a8')
|
||||||
@@ -49,14 +58,10 @@ export class WeatherApp extends React.Component {
|
|||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then((responseObject) => {
|
.then((responseObject) => {
|
||||||
this.data = responseObject as Forecast5Days;
|
this.setState({ data: responseObject as Forecast5Days });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
isDataLoaded():this.data is Forecast5Days {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
@@ -65,7 +70,7 @@ export class WeatherApp extends React.Component {
|
|||||||
<h1 className="App-title">Welcome to React</h1>
|
<h1 className="App-title">Welcome to React</h1>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
{this.data!==undefined ? }
|
{this.state.data !== undefined ? JSON.stringify(this.state.data) : 'Data Loading...'}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user