[docs] useRouter
cannot be used with classes (#8649)
The `useRouter` function is a hook and cannot be used with Classes. A few React beginners have brought this up, so we document the alternatives to hooks.
This commit is contained in:
parent
6444889c9b
commit
73b6c1e633
1 changed files with 4 additions and 1 deletions
|
@ -955,7 +955,7 @@ componentDidUpdate(prevProps) {
|
|||
</ul>
|
||||
</details>
|
||||
|
||||
If you want to access the `router` object inside any component in your app, you can use the `useRouter` hook, here's how to use it:
|
||||
If you want to access the `router` object inside any functional component in your app, you can use the `useRouter` hook, here's how to use it:
|
||||
|
||||
```jsx
|
||||
import { useRouter } from 'next/router'
|
||||
|
@ -980,6 +980,9 @@ export default function ActiveLink({ children, href }) {
|
|||
}
|
||||
```
|
||||
|
||||
> **Note**: `useRouter` is a React hook, meaning it cannot be used with classes.
|
||||
> You can either use [`withRouter`](#using-a-higher-order-component) (a higher order component) or wrap your class in a functional component.
|
||||
|
||||
The above `router` object comes with an API similar to [`next/router`](#imperatively).
|
||||
|
||||
#### Using a Higher Order Component
|
||||
|
|
Loading…
Reference in a new issue