01779429ab
Not sure when it was introduced, but on the latest Next v8.0.4, there's no descriptive name attached to the component returns from `withRouter`. This makes it harder to debug and write test cases for components wrapped by `withRouter`.
27 lines
775 B
TypeScript
27 lines
775 B
TypeScript
import React from 'react'
|
|
import PropTypes from 'prop-types'
|
|
|
|
export default function withRouter(ComposedComponent: React.ComponentType<any> & {getInitialProps?: any}) {
|
|
class WithRouteWrapper extends React.Component {
|
|
static displayName?: string
|
|
static getInitialProps?: any
|
|
static contextTypes = {
|
|
router: PropTypes.object,
|
|
}
|
|
|
|
render() {
|
|
return <ComposedComponent
|
|
router={this.context.router}
|
|
{...this.props}
|
|
/>
|
|
}
|
|
}
|
|
|
|
WithRouteWrapper.getInitialProps = ComposedComponent.getInitialProps
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
const name = ComposedComponent.displayName || ComposedComponent.name || 'Unknown'
|
|
WithRouteWrapper.displayName = `withRouter(${name})`
|
|
}
|
|
|
|
return WithRouteWrapper
|
|
}
|