import * as React from 'react'; import { connect } from 'react-redux'; import { RouteComponentProps } from 'react-router'; import { Link } from 'react-router-dom'; import { ApplicationState } from '../store'; import * as WeatherForecastsStore from '../store/WeatherForecasts'; // At runtime, Redux will merge together... type WeatherForecastProps = WeatherForecastsStore.WeatherForecastsState // ... state we've requested from the Redux store & typeof WeatherForecastsStore.actionCreators // ... plus action creators we've requested & RouteComponentProps<{ startDateIndex: string }>; // ... plus incoming routing parameters class FetchData extends React.PureComponent { // This method is called when the component is first added to the document public componentDidMount() { this.ensureDataFetched(); } // This method is called when the route parameters change public componentDidUpdate() { this.ensureDataFetched(); } public render() { return (

Weather forecast

This component demonstrates fetching data from the server and working with URL parameters.

{this.renderForecastsTable()} {this.renderPagination()}
); } private ensureDataFetched() { const startDateIndex = parseInt(this.props.match.params.startDateIndex, 10) || 0; this.props.requestWeatherForecasts(startDateIndex); } private renderForecastsTable() { return ( {this.props.forecasts.map((forecast: WeatherForecastsStore.WeatherForecast) => )}
Date Temp. (C) Temp. (F) Summary
{forecast.date} {forecast.temperatureC} {forecast.temperatureF} {forecast.summary}
); } private renderPagination() { const prevStartDateIndex = (this.props.startDateIndex || 0) - 5; const nextStartDateIndex = (this.props.startDateIndex || 0) + 5; return (
Previous {this.props.isLoading && Loading...} Next
); } } export default connect( (state: ApplicationState) => state.weatherForecasts, // Selects which state properties are merged into the component's props WeatherForecastsStore.actionCreators // Selects which action creators are merged into the component's props )(FetchData as any);