rsnext/packages/next/client/with-router.tsx
Simon Boudrias 01779429ab Set descriptive displayName on withRouter HOC (#7017)
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`.
2019-04-13 13:21:28 +02:00

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
}