Skip to content

Breaking type change on useRouter() in v13.0.2-canary.0 #42409

@jakejarvis

Description

@jakejarvis

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103
Binaries:
  Node: 16.18.0
  npm: 8.19.2
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 13.0.2-canary.2
  eslint-config-next: 13.0.2-canary.2
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

A breaking change seems to be introduced in #41767 now that useRouter returns NextRouter | null instead of NextRouter by default. Calling router.pathname or router.events, for instance, throws a Type error: Object is possibly 'null' error.

Switching instances of useRouter() to useRouter(true) was an easy enough fix, but I expect lots more people will be hitting this speed bump when this change is released.

Expected Behavior

Is there a smart way to make this a non-breaking change? If this behavior is expected to be breaking, feel free to close this (but I'd still suggest emphasizing this change in the release notes to avoid a flood of bug reports! 😉)

Link to reproduction

vercel/examples#443 (review)

To Reproduce

Update from 13.0.1 to 13.0.2-canary.0 and up.

To fix with minimal changes, replace instances of useRouter() with useRouter(true).

Related: vercel/examples#443

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions