import React from 'react' import { NextComponentType, NextPageContext } from '../next-server/lib/utils' import { NextRouter, RouterContext } from './router' export type WithRouterProps = { router: NextRouter } export type ExcludeRouterProps

= Pick< P, Exclude > export default function withRouter< P extends WithRouterProps, C = NextPageContext >( ComposedComponent: NextComponentType ): React.ComponentClass> { class WithRouteWrapper extends React.Component> { static displayName?: string static getInitialProps?: any static contextType = RouterContext context!: React.ContextType render() { return } } WithRouteWrapper.getInitialProps = ComposedComponent.getInitialProps // This is needed to allow checking for custom getInitialProps in _app ;(WithRouteWrapper as any).origGetInitialProps = (ComposedComponent as any).origGetInitialProps if (process.env.NODE_ENV !== 'production') { const name = ComposedComponent.displayName || ComposedComponent.name || 'Unknown' WithRouteWrapper.displayName = `withRouter(${name})` } return WithRouteWrapper }