|
1 | | -import * as React from 'react'; |
2 | | - |
3 | | -interface FetchDataExampleState { |
4 | | - forecasts: WeatherForecast[]; |
5 | | - loading: boolean; |
6 | | -} |
7 | | - |
8 | | -export class FetchData extends React.Component<any, FetchDataExampleState> { |
9 | | - constructor() { |
10 | | - super(); |
11 | | - this.state = { forecasts: [], loading: true }; |
12 | | - |
13 | | - fetch('/api/SampleData/WeatherForecasts') |
14 | | - .then(response => response.json() as Promise<WeatherForecast[]>) |
15 | | - .then(data => { |
16 | | - this.setState({ forecasts: data, loading: false }); |
17 | | - }); |
18 | | - } |
19 | | - |
20 | | - public render() { |
21 | | - let contents = this.state.loading |
22 | | - ? <p><em>Loading...</em></p> |
23 | | - : FetchData.renderForecastsTable(this.state.forecasts); |
24 | | - |
25 | | - return <div> |
26 | | - <h1>Weather forecast</h1> |
27 | | - <p>This component demonstrates fetching data from the server.</p> |
28 | | - { contents } |
29 | | - <p>For more sophisticated applications, consider an architecture such as Redux or Flux for managing state. You can generate an ASP.NET Core application with React and Redux using <code>dotnet new aspnet/spa/reactredux</code> instead of using this template.</p> |
30 | | - </div>; |
31 | | - } |
32 | | - |
33 | | - private static renderForecastsTable(forecasts: WeatherForecast[]) { |
34 | | - return <table className='table'> |
35 | | - <thead> |
36 | | - <tr> |
37 | | - <th>Date</th> |
38 | | - <th>Temp. (C)</th> |
39 | | - <th>Temp. (F)</th> |
40 | | - <th>Summary</th> |
41 | | - </tr> |
42 | | - </thead> |
43 | | - <tbody> |
44 | | - {forecasts.map(forecast => |
45 | | - <tr key={ forecast.dateFormatted }> |
46 | | - <td>{ forecast.dateFormatted }</td> |
47 | | - <td>{ forecast.temperatureC }</td> |
48 | | - <td>{ forecast.temperatureF }</td> |
49 | | - <td>{ forecast.summary }</td> |
50 | | - </tr> |
51 | | - )} |
52 | | - </tbody> |
53 | | - </table>; |
54 | | - } |
55 | | -} |
56 | | - |
57 | | -interface WeatherForecast { |
58 | | - dateFormatted: string; |
59 | | - temperatureC: number; |
60 | | - temperatureF: number; |
61 | | - summary: string; |
62 | | -} |
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +interface FetchDataExampleState { |
| 4 | + forecasts: WeatherForecast[]; |
| 5 | + loading: boolean; |
| 6 | +} |
| 7 | + |
| 8 | +export class FetchData extends React.Component<any, FetchDataExampleState> { |
| 9 | + constructor() { |
| 10 | + super(); |
| 11 | + this.state = { forecasts: [], loading: true }; |
| 12 | + |
| 13 | + fetch('/api/SampleData/WeatherForecasts') |
| 14 | + .then(response => response.json() as Promise<WeatherForecast[]>) |
| 15 | + .then(data => { |
| 16 | + this.setState({ forecasts: data, loading: false }); |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + public render() { |
| 21 | + let contents = this.state.loading |
| 22 | + ? <p><em>Loading...</em></p> |
| 23 | + : FetchData.renderForecastsTable(this.state.forecasts); |
| 24 | + |
| 25 | + return <div> |
| 26 | + <h1>Weather forecast</h1> |
| 27 | + <p>This component demonstrates fetching data from the server.</p> |
| 28 | + { contents } |
| 29 | + <p>For more sophisticated applications, consider an architecture such as Redux or Flux for managing state. You can generate an ASP.NET Core application with React and Redux using <code>dotnet new aspnet/spa/reactredux</code> instead of using this template.</p> |
| 30 | + </div>; |
| 31 | + } |
| 32 | + |
| 33 | + private static renderForecastsTable(forecasts: WeatherForecast[]) { |
| 34 | + return <table className='table'> |
| 35 | + <thead> |
| 36 | + <tr> |
| 37 | + <th>Date</th> |
| 38 | + <th>Temp. (C)</th> |
| 39 | + <th>Temp. (F)</th> |
| 40 | + <th>Summary</th> |
| 41 | + </tr> |
| 42 | + </thead> |
| 43 | + <tbody> |
| 44 | + {forecasts.map(forecast => |
| 45 | + <tr key={ forecast.dateFormatted }> |
| 46 | + <td>{ forecast.dateFormatted }</td> |
| 47 | + <td>{ forecast.temperatureC }</td> |
| 48 | + <td>{ forecast.temperatureF }</td> |
| 49 | + <td>{ forecast.summary }</td> |
| 50 | + </tr> |
| 51 | + )} |
| 52 | + </tbody> |
| 53 | + </table>; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +interface WeatherForecast { |
| 58 | + dateFormatted: string; |
| 59 | + temperatureC: number; |
| 60 | + temperatureF: number; |
| 61 | + summary: string; |
| 62 | +} |
0 commit comments