Unverified Commit 73b6c1e6 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

[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.
parent 6444889c
...@@ -955,7 +955,7 @@ componentDidUpdate(prevProps) { ...@@ -955,7 +955,7 @@ componentDidUpdate(prevProps) {
</ul> </ul>
</details> </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 ```jsx
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
...@@ -980,6 +980,9 @@ export default function ActiveLink({ children, href }) { ...@@ -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). The above `router` object comes with an API similar to [`next/router`](#imperatively).
#### Using a Higher Order Component #### Using a Higher Order Component
......
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