[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:
Joe Haddad 2019-09-05 23:05:59 -04:00 committed by GitHub
parent 6444889c9b
commit 73b6c1e633
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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