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.

`router.isReady` is not working correctly with React 18

See original GitHub issue

Verify canary release

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

Provide environment information

Operating System:
  Platform: OSX
  Arch: ARM
  Version: MacOS Monterey
Binaries:
  Node: 16.14.0
  npm: N/A
  Yarn: N/A
  pnpm: 6.32.3
Relevant packages:
  next: 12.1.3-canary.4
  react: 18.0.0
  react-dom: 18.0.0

What browser are you using? (if relevant)

Chrome 101.0.4951.15

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

When I boot the next.js project in local with React 18, router.isReady returns false if the URL has a query value. When I deploy the project with Vercel, there is no such problem. The problem only occurs in local.

Expected Behavior

The isReady query returns true when the page is loaded if the project is started in localhost.

To Reproduce

Example code:

import { useRouter } from 'next/router'
import {useEffect, useState} from "react";

export default function Home() {
    const router = useRouter()

    const [isReady, setIsReady] = useState(router.isReady)
    const [hasMounted, setHasMounted] = useState(false)

    // Set router.isReady when the page has mounted
    useEffect(() => {
        setIsReady(router.isReady)
    }, [router])

    // Blocking hydration warning
    useEffect(() => {
        setHasMounted(true)
    }, [])
    if (!hasMounted) {
        return null
    }

    const addQuery = (query) => {
        router.push({
            pathname: '/',
            query: query ? {query: query} : null
        }).then(()=>router.reload())
    }
    return (
        <div>
        <h2>{ isReady ? "Route is ready" : "Route is not ready"}</h2>
        <button onClick={()=>addQuery("1okok3mkn3mjf7f")}>Add query to url</button>
        <button onClick={()=>addQuery(null)}>Delete query from url</button>
        </div>
    )
}


Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
iuzncommented, Mar 31, 2022

Yes, I updated. It now works properly. Thanks, @ijjk

1reaction
ijjkcommented, Mar 31, 2022

Hi, this has been updated in the latest version of Next.js v12.1.4-canary.1, please update and give it a try!

Read more comments on GitHub >

github_iconTop Results From Across the Web

useRouter/withRouter receive undefined on query in first render
I found something: isReady: boolean - Whether the router fields are updated client-side and ready for use. Should only be used inside of ......
Read more >
Suspense for Data Fetching (Experimental) - React
We call this approach “fetch-on-render” because it doesn't start fetching until after the component has rendered on the screen. This leads to a...
Read more >
Experimental React: Using Suspense for data fetching
The implementation of the fetchData function, the Spinner , and Todos components are not relevant here. A variable, isLoading , is used to...
Read more >
Working With Next.js Router's Query - Thomas Step
The useEffect hook only fires whenever isReady updates. This means it will fire on the initial page load when router is not ready,...
Read more >
React Query meets React Router | TkDodo's blog
Nah, React Router is not a cache. Browsers have this built in with HTTP and libraries like React Query have this job nailed...
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