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

Bug: putting promise into JSX will cause hydration issue #27778

Closed
himself65 opened this issue Dec 3, 2023 · 13 comments
Closed

Bug: putting promise into JSX will cause hydration issue #27778

himself65 opened this issue Dec 3, 2023 · 13 comments
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@himself65
Copy link
Contributor

himself65 commented Dec 3, 2023

React version: 18.3.0-canary-5dd35968b-20231201

Steps To Reproduce

  1. clone https://github.com/himself65/react-hydration-error
  2. pnpm install && pnpm run dev

Link to code example:

import { Component } from '@/components/component'

export default function Home () {
  const delayedMessage = new Promise((resolve) => {
    setTimeout(() => resolve('Hello from server!'), 2000)
  })
  return (
    <main>
      <div>
        <Component delayedMessage={delayedMessage}/>
      </div>
    </main>
  )
}
"use client"
export function Component ({
  delayedMessage
}) {
  return (
    <div>
      {delayedMessage}
    </div>
  )
}

The current behavior

image

The expected behavior

Won't have hydration error

@himself65 himself65 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Dec 3, 2023
@himself65
Copy link
Contributor Author

Related downstream repo: dai-shi/waku#203

@himself65
Copy link
Contributor Author

dai-shi/waku#202

@himself65 himself65 changed the title Bug: putting promise into JSX props will cause hydration issue Bug: putting promise into JSX will cause hydration issue Dec 3, 2023
@himself65
Copy link
Contributor Author

I think this is a React issue, not next.js. Since our customized react framework has the same error

@NakZMichael
Copy link

NakZMichael commented Dec 4, 2023

Prop values passed from a Server Component to Client Component must be serializable.

https://react.dev/reference/react/use-client

I think using async/await works well.

Sorry, Promise is serealizable. I misunderstood.

@NakZMichael
Copy link

Values of serialized promises are different between client side and server side.

@godspeed-03
Copy link

The issue can be resolved by using useEffect hook .
Head to next js documentation.

@NakZMichael
Copy link

I found a workaround for this matter. It is to use use to Promises.
https://github.com/NakZMichael/react-hydration-error

However, looking at the following PR, it appears that this workaround is not supposed to be necessary.
#25634

And it seems to be related to the following Issue discussed in the Next.js repository.
vercel/next.js#56470

@himself65
Copy link
Contributor Author

Since promise is a valid jsx element, I think it should not cause ssr issues or otherwise should be documented

@MadhuSaini22
Copy link

@himself65 , Thanks for reporting the hydration issue with promises in JSX. Putting promises directly in JSX can be tricky in React 18.
Here's a quick fix:

Render a placeholder on the server while the promise resolves on the client (e.g., a loading spinner). Update the DOM with the resolved value once it's available.
Remember to update the server-side HTML with the resolved value for optimal performance.

For more advanced handling, consider Suspense or data fetching libraries like react-query or SWR.

Happy coding!

Copy link

github-actions bot commented Apr 5, 2024

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 5, 2024
@himself65
Copy link
Contributor Author

bump

@github-actions github-actions bot removed the Resolution: Stale Automatically closed due to inactivity label Apr 10, 2024
Copy link

github-actions bot commented Jul 9, 2024

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Jul 9, 2024
Copy link

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

4 participants