import * as React from 'react'; import { Link } from 'react-router'; import { connect } from 'react-redux'; import { ApplicationState } from '../store'; import * as WeatherForecastsState from '../store/WeatherForecasts'; // At runtime, Redux will merge together... type WeatherForecastProps = WeatherForecastsState.WeatherForecastsState // ... state we've requested from the Redux store & typeof WeatherForecastsState.actionCreators // ... plus action creators we've requested & { params: { startDateIndex: string } }; // ... plus incoming routing parameters class FetchData extends React.Component { componentWillMount() { // This method runs when the component is first added to the page let startDateIndex = parseInt(this.props.params.startDateIndex) || 0; this.props.requestWeatherForecasts(startDateIndex); } componentWillReceiveProps(nextProps: WeatherForecastProps) { // This method runs when incoming props (e.g., route params) change let startDateIndex = parseInt(nextProps.params.startDateIndex) || 0; this.props.requestWeatherForecasts(startDateIndex); } public render() { return

Weather forecast

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

{ this.renderForecastsTable() } { this.renderPagination() }
; } private renderForecastsTable() { return {this.props.forecasts.map(forecast => )}
Date Temp. (C) Temp. (F) Summary
{ forecast.dateFormatted } { forecast.temperatureC } { forecast.temperatureF } { forecast.summary }
; } private renderPagination() { let prevStartDateIndex = this.props.startDateIndex - 5; let nextStartDateIndex = this.props.startDateIndex + 5; return

Previous Next { this.props.isLoading ? Loading... : [] }

; } } export default connect( (state: ApplicationState) => state.weatherForecasts, // Selects which state properties are merged into the component's props WeatherForecastsState.actionCreators // Selects which action creators are merged into the component's props )(FetchData);