Unverified Commit 6c51ad96 authored by Faizan Virani's avatar Faizan Virani Committed by GitHub

Added Typescript example for withRouter (#18116)

Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 130039c3
......@@ -402,3 +402,26 @@ function Page({ router }) {
export default withRouter(Page)
```
### Typescript
To use class components with `withRouter`, the component needs to accept a router prop:
```tsx
import React from 'react'
import { withRouter, NextRouter } from 'next/router'
interface WithRouterProps {
router: NextRouter
}
interface MyComponentProps extends WithRouterProps {}
class MyComponent extends React.Component<MyComponentProps> {
render() {
return <p>{this.props.router.pathname}</p>
}
}
export default withRouter(MyComponent)
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment