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.

Next.JS / React 18 - Hydration Error

See original GitHub issue

Current Behavior

Unless react-player is loaded on the client-side using dynamic/no-ssr or useEffect, React will panic with a hydration error.

Expected Behavior

Use of SSR should not result in a hydration error.

Steps to Reproduce

  1. Using Next.JS, import react-player and follow typical implementation instructions.
  2. Error

Other Information

There is a great thread on Stack Overflow here (https://stackoverflow.com/questions/71706064/react-18-hydration-failed-because-the-initial-ui-does-not-match-what-was-render) where devs list various solutions to related issues.

Examples:

  • react and react-dom may need to be updated.
  • Wrapping tags improperly, such as <div> inside <p> will cause the error.
  • Lazy modules need to be wrapped in <Suspense>.

Please check again these scenarios in the codebase. Thank you!

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:13
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

28reactions
karlhorkycommented, Jul 14, 2022

I agree that ReactPlayer should be SSR-friendly out of the box, @cookpete what do you think?


For now, until that happens, another workaround is to lazy-load the player using Next.js dynamic imports, as mentioned by @inderrr in this comment:

import dynamic from 'next/dynamic';

const ReactPlayer = dynamic(() => import('react-player/lazy'), { ssr: false });
6reactions
narasakacommented, Jul 9, 2022

Have you tried this?

It’s basically checking the window.

const Component: React.FC<SomeProps> = ({url}) =>{
  const [hasWindow, setHasWindow] = useState(false);
  useEffect(() => {
    if (typeof window !== "undefined") {
      setHasWindow(true);
    }
  }, []);
  return (
    <>
      {hasWindow && <ReactPlayer url={url} />}
    </>
  )
Read more comments on GitHub >

github_iconTop Results From Across the Web

react-hydration-error - Next.js
In general this issue is caused by using a specific library or application code that is relying on something that could differ between...
Read more >
React 18: Hydration failed because the initial UI does not ...
I have been experiencing the same problem lately with NextJS and i am not sure if my observations are applicable to other libraries....
Read more >
Hydration failed because the initial UI does not match ... - GitHub
It's likely that you have a mismatch between the server output and the client. Before React 18, you'd only get an error in...
Read more >
How To Solve Hydration Error In Next.js | by Westin Tang
The reason for the error has been clearly stated in the error message: the hydration failed due to the inconsistency between the UI...
Read more >
Fix Next.js “Text content does not match server-rendered ...
Learn how to fix the React hydration error “Text content does not match server-rendered HTML” so you can personalize & transform HTML in ......
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