Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router.isReady is not working correctly with React 18 #35754

Closed
1 task done
iuzn opened this issue Mar 30, 2022 · 6 comments · Fixed by #35762
Closed
1 task done

router.isReady is not working correctly with React 18 #35754

iuzn opened this issue Mar 30, 2022 · 6 comments · Fixed by #35762
Labels
bug Issue was opened via the bug report template.

Comments

@iuzn
Copy link

iuzn commented Mar 30, 2022

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>
    )
}
@iuzn iuzn added the bug Issue was opened via the bug report template. label Mar 30, 2022
@devknoll
Copy link
Contributor

devknoll commented Mar 30, 2022

Could you please provide a full reproduction (via GitHub or CodeSandbox)? I tried your example using the versions specified and I'm seeing Route is ready rendered when running locally (both via next dev and next build + next start).

@iuzn
Copy link
Author

iuzn commented Mar 31, 2022

I don't understand why this problem is caused. router.isReady works fine in some cases. But it doesn't work properly in local.

I prepared a sample project on GitHub: https://github.com/iuzn/next-router-with-react-18

CodeSandbox: https://codesandbox.io/s/next-router-with-react-18-eu4r7u

I'm wondering where I went wrong. I've been checking everything one by one for hours.

@ijjk
Copy link
Member

ijjk commented Mar 31, 2022

Hi, I was able to reproduce this with the above repo, the next.config.js being present seemed to be an important part, thanks for providing that! A fix for this has been opened here #35762

@kodiakhq kodiakhq bot closed this as completed in #35762 Mar 31, 2022
kodiakhq bot pushed a commit that referenced this issue Mar 31, 2022
…5762)

This fixes `router.isReady` being incorrect in dev mode due to the `isAutoExport` field being false from `hasConcurrentFeatures` being flagged similar to the static 404 in #35749. While investigating this I also noticed we aren't properly detecting react 18 when no `next.config.js` is present. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: #35754
x-ref: #35749
@ijjk
Copy link
Member

ijjk commented 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!

@iuzn
Copy link
Author

iuzn commented Mar 31, 2022

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

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 30, 2022
pavleadev added a commit to pavleadev/next.js that referenced this issue Mar 29, 2023
…5762)

This fixes `router.isReady` being incorrect in dev mode due to the `isAutoExport` field being false from `hasConcurrentFeatures` being flagged similar to the static 404 in vercel/next.js#35749. While investigating this I also noticed we aren't properly detecting react 18 when no `next.config.js` is present. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: vercel/next.js#35754
x-ref: vercel/next.js#35749
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants