db5b3dc8e3
* Updated declarations for with-router
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import React from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import { NextComponentType, IContext } from 'next-server/dist/lib/utils'
|
|
import { IPublicRouterInstance } from './router';
|
|
|
|
export type WithRouterProps = {
|
|
router: IPublicRouterInstance,
|
|
}
|
|
|
|
export type ExcludeRouterProps<P> = Pick<P, Exclude<keyof P, keyof WithRouterProps>>
|
|
|
|
export default function withRouter<P extends WithRouterProps, C = IContext>(ComposedComponent: NextComponentType<C, any, P>): React.ComponentClass<ExcludeRouterProps<P>> {
|
|
class WithRouteWrapper extends React.Component<ExcludeRouterProps<P>> {
|
|
static displayName?: string
|
|
static getInitialProps?: any
|
|
static contextTypes = {
|
|
router: PropTypes.object,
|
|
}
|
|
|
|
context!: WithRouterProps
|
|
|
|
render() {
|
|
return <ComposedComponent
|
|
router={this.context.router}
|
|
{...this.props as any}
|
|
/>
|
|
}
|
|
}
|
|
|
|
WithRouteWrapper.getInitialProps = ComposedComponent.getInitialProps
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
const name = ComposedComponent.displayName || ComposedComponent.name || 'Unknown'
|
|
WithRouteWrapper.displayName = `withRouter(${name})`
|
|
}
|
|
|
|
return WithRouteWrapper
|
|
}
|