Skip to content

Commit

Permalink
expanded on No Router Instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Robogeek95 committed Jan 17, 2021
1 parent ce5d9c8 commit 1aa32d4
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion errors/no-router-instance.md
Expand Up @@ -2,8 +2,13 @@

#### Why This Error Occurred

Next.js is universal, which means it executes code first server-side with NodeJS, then on the client-side, `window` object is not defined in NodeJS, so some methods are not supported or availale at build time.
During SSR you might have tried to access a router method `push`, `replace`, `back`, which is not supported.

#### Possible Ways to Fix It

Move any calls to router methods to `componentDidMount` or add a check such as `typeof window !== 'undefined'` before calling the methods
In a class Component, move any calls to router methods to `componentDidMount` lifecycle method or add a check such as `typeof window !== 'undefined'` before calling the methods

In a functional Component you can move the code into the `useEffect` hook. checking `typeof window !== 'undefined'` also works in a functional component.

This way the calls to the router methods are only executed on the client (in the browser), thus ensuring access to the `window` object.

0 comments on commit 1aa32d4

Please sign in to comment.