import * as React from 'react'; import * as PropTypes from 'prop-types'; import isEqual = require('deep-equal'); import { Component, ValidationMap } from 'react'; import { History, Location } from 'history'; import { Store, RouterStoreState } from './Store'; export type RouterProviderProps = { history: History; location: Location; }; export type RouterProviderChildContext = { router: { history: History }; routerStore: Store; }; export class RouterProvider extends Component { static displayName: string = 'RouterProvider'; static childContextTypes: ValidationMap = { routerStore: PropTypes.instanceOf(Store), router: PropTypes.any, }; private routerStore: Store; constructor(props: RouterProviderProps) { super(props); this.routerStore = new Store({ location: props.location, match: null, switch: false, }); } UNSAFE_componentWillReceiveProps(nextProps: RouterProviderProps): void { if (!isEqual(nextProps.location, this.props.location)) { this.routerStore.setState({ location: nextProps.location, match: null, switch: false, }); } } getChildContext(): RouterProviderChildContext { return { router: { history: this.props.history }, routerStore: this.routerStore, }; } render(): JSX.Element { return React.Children.only(this.props.children); } }