Skip to content

Commit f176683

Browse files
authored
Added section about router methods returning a promise (vercel#31341)
This PR adds a section on certain router methods returning a promise, and the error you will see if you have no-floating-promises enabled. ## Bug - [x] Related issues linked using fixes: vercel#31337 - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
1 parent 6ffa2ba commit f176683

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

docs/api-reference/next/router.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The following is the definition of the `router` object returned by both [`useRou
5353
- `isReady`: `boolean` - Whether the router fields are updated client-side and ready for use. Should only be used inside of `useEffect` methods and not for conditionally rendering on the server.
5454
- `isPreview`: `boolean` - Whether the application is currently in [preview mode](/docs/advanced-features/preview-mode.md).
5555

56-
Additionally, the following methods are also included inside `router`:
56+
The following methods are included inside `router`:
5757

5858
### router.push
5959

@@ -414,6 +414,53 @@ export default function MyApp({ Component, pageProps }) {
414414
}
415415
```
416416

417+
## Potential ESLint errors
418+
419+
Certain methods accessible on the `router` object return a Promise. If you have the ESLint rule, [no-floating-promises](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-floating-promises.md) enabled, consider disabling it either globally, or for the affected line.
420+
421+
If your application needs this rule, you should either `void` the promise – or use an `async` function, `await` the Promise, then void the function call. **This is not applicable when the method is called from inside an `onClick` handler**.
422+
423+
The affected methods are:
424+
425+
- `router.push`
426+
- `router.replace`
427+
- `router.prefetch`
428+
429+
### Potential solutions
430+
431+
```jsx
432+
import { useEffect } from 'react'
433+
import { useRouter } from 'next/router'
434+
435+
// Here you would fetch and return the user
436+
const useUser = () => ({ user: null, loading: false })
437+
438+
export default function Page() {
439+
const { user, loading } = useUser()
440+
const router = useRouter()
441+
442+
useEffect(() => {
443+
// disable the linting on the next line - This is the cleanest solution
444+
// eslint-disable-next-line no-floating-promises
445+
router.push('/login')
446+
447+
// void the Promise returned by router.push
448+
if (!(user || loading)) {
449+
void router.push('/login')
450+
}
451+
// or use an async function, await the Promise, then void the function call
452+
async function handleRouteChange() {
453+
if (!(user || loading)) {
454+
await router.push('/login')
455+
}
456+
}
457+
void handleRouteChange()
458+
}, [user, loading])
459+
460+
return <p>Redirecting...</p>
461+
}
462+
```
463+
417464
## withRouter
418465

419466
If [`useRouter`](#useRouter) is not the best fit for you, `withRouter` can also add the same [`router` object](#router-object) to any component.

0 commit comments

Comments
 (0)