question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Object(...) is not a function - with useRouter()

See original GitHub issue

Bug report

Describe the bug

I am visiting a dynamic page. The page is a hook. The first line in the hook is const router = useRouter(). This works 9 out of 10 times, but sometimes it will say TypeError: Object(...) is not a function on this line. It happens sometimes when navigating to said page, but sometimes when loading the page fresh without navigating there.

To Reproduce

Unable to reproduce in fresh/external project, so I think something I am doing locally is breaking it, I am however unsure what specifically.

Local code example in project where it sometimes will not work:

/pages/levels/[dynamicPageSlug]/index.js

import { useRouter } from 'next/router'
import React from 'react';

export default function DynamicPage() {
  const router = useRouter()   // crashes here sporadically

  const {dynamicPageSlug} = router.query;

  if (!dynamicPageSlug)  { <p> loading </p>}

  return <p>{dynamicPageSlug}</p>
}

Is there anything that can happen elsewhere in the application which could invoke such behaviour? Thanks. I am also open to suggestions how to solve this kind of scenario better.

Expected behavior

It should not crash, but continue. It should give back the loading paragraph and continue clientside and print the slug.

System information

  • OS: macOS
  • Version of Next.js: 9.5.5
  • Version of Node.js: Node 12

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
leonardoanalistacommented, Jan 5, 2021

I accidentally upgraded react to version 17. It worked in my local “npm dev” also “npm build && npm start” but it failed to run at Vercel.

The way I fixed everywhere was to downgrade to version 16:

"react": "^16.7.0",
"react-dom": "^16.7.0"
0reactions
github-actions[bot]commented, Feb 21, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React router error "TypeError: Object(...) is not a function"
im currently learning react. During course about routing i get this error Screenshoot I' ve been making sure that i' ve write code...
Read more >
Uncaught TypeError: Object(...) is not a function - Laracasts
Uncaught TypeError: Object(...) is not a function. While I am going to add Vue Router it is giving me this error and router-link...
Read more >
next/router | Next.js
useRouter is a React Hook, meaning it cannot be used with classes. You can either use withRouter or wrap your class in a...
Read more >
ReactJs: TypeError: Object(...) is not a function - SitePoint
Hello everyone, Today I started a a new react app and on one of my components I got the following error: The code...
Read more >
useHooks - Easy to understand React Hook recipes
import { useCallback, useState } from 'react'; // Usage function App() ... If the compare function returns true then the hook returns the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found