Load data into state

This commit is contained in:
2018-03-30 06:08:48 +05:30
parent c4a05e31c7
commit 30f8217fa3
+13 -8
View File
@@ -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>
); );
} }