2019-01-11 15:04:56 +00:00
|
|
|
import React from 'react'
|
2017-08-30 14:07:12 +00:00
|
|
|
import hoistStatics from 'hoist-non-react-statics'
|
2018-11-28 14:03:02 +00:00
|
|
|
import { getDisplayName } from 'next-server/dist/lib/utils'
|
2019-01-11 15:04:56 +00:00
|
|
|
import { RouterContext } from './router'
|
2017-08-30 14:07:12 +00:00
|
|
|
|
2017-08-31 19:37:20 +00:00
|
|
|
export default function withRouter (ComposedComponent) {
|
2017-08-30 14:07:12 +00:00
|
|
|
const displayName = getDisplayName(ComposedComponent)
|
|
|
|
|
2019-01-11 15:04:56 +00:00
|
|
|
function WithRouteWrapper (props) {
|
|
|
|
return (
|
|
|
|
<RouterContext.Consumer>
|
|
|
|
{router => (
|
|
|
|
<ComposedComponent
|
|
|
|
router={router}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</RouterContext.Consumer>
|
|
|
|
)
|
2017-08-30 14:07:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 15:04:56 +00:00
|
|
|
WithRouteWrapper.displayName = `withRouter(${displayName})`
|
|
|
|
|
2017-08-30 14:07:12 +00:00
|
|
|
return hoistStatics(WithRouteWrapper, ComposedComponent)
|
|
|
|
}
|