-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Next.JS / React 18 - Hydration Error #1474
Comments
I have the same issue and I get this error while using even when I set a preview image for light mode and no change happens in UI during hydration, I get the hydration error. |
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} />}
</>
) |
For temporary solution you can downgrade react to 17.0.2 for example |
I've tried this and of course it works! but this means rendering the react-player component on the client side so I need to render another thing ( like an image) on the server side. I wish the react-player would handle the SSR by itself |
I just found out that in ReactPlayer.js the window is used in the component's rendering which is not recommended in this document and leads to Hydration Error |
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 }); |
Sounds great. I just don’t have the time or knowledge to implement, test and document it. |
Can you upgrade |
The versions of the
If you want to submit a PR, then probably best would be to:
https://dev.to/vvo/how-to-solve-window-is-not-defined-errors-in-react-and-next-js-5f97
react-player/src/ReactPlayer.js Lines 12 to 18 in 9775bb7
But |
Hi, |
@yogesh2503 this works fine: #1474 (comment) |
Thank |
🎟️ [Asana Task]() 🔍 [Preview Link](https://react-components-git-{branch-slug}-hashicorp.vercel.app) --- <!-- Reminder: This is an open source project, make sure not to include any sensitive information in the pull request. --> ## Description Unless `react-player` is loaded on the client-side using dynamic/no-ssr or `useEffect`, React will panic with a hydration error (see [this issue](cookpete/react-player#1474)). This PR updates the `InlineVideo` component to conditionally render the player if the `window` not `undefined`. ### PR Checklist 🚀 Items in this checklist may not may not apply to your PR, but please consider each item carefully. - [ ] Add Asana and Preview links above. - [ ] Conduct thorough self-review. - [ ] Add or update tests as appropriate. - [ ] Conduct reasonable cross browser testing for both compatibility and responsive behavior (We have a [Sauce Labs](https://app.saucelabs.com/) account for this, if you don't have access, just ask!). - [ ] Conduct reasonable accessibility review (use the [WAS](https://accessible.org/Web-Accessibility-Standards-WAS-2.pdf) as a guide or an [axe browser plugin](https://www.deque.com/axe/) until we establish more formal checks). - [ ] Identify (in the description above) and document (add Asana tasks on [this board](https://app.asana.com/0/1100423001970639/list)) any technical debt that you're aware of, but are not addressing as part of this PR.
Its work for me To dynamically load a component on the client side, you can use the ssr option to disable server-rendering. This is useful if an external dependency or component relies on browser APIs like window. import dynamic from 'next/dynamic' const DynamicHeader = dynamic(() => import('../components/header'), { |
Thank you, just what I was looking for 👍 |
Ok you were definitely on to something with this and it took me some digging but I think I figured out why this is so incompatible with SSR. There was one bit of magic that I noticed in the Readme that struck me as odd but I couldn't figure out why. For Youtube embeds, there is an example of the actual url of the YT video, not the embed url or the id of the video. In other implementations I've seen or done for YT embeds, typically there is some more configuration needed around stripping the embed id or specifying the poster image (for a "light" version) but this handled it. In the line you references, we are returning null on the server side, since Suspense doesn't work server side pre- React 18. I think now we could probably set a flag or do a check for the React version to enable Suspense/lazy. But that just allows the code to attempt to run in SSR. The next piece I've seen is the call to window in the file Line 42 in 662082a
Basically, if the url is for youtube and a poster image isn't provided, we are fetching a YT endpoint that provides some meta about the video, including a poster image uri and html for an iframe. I'm going to make an attempt to fix this in a universal way in a fork I made, but one big thought is on my mind: Should we be doing it this way? I think there might be a solution that is right enough for all React environments, but ultimately if this component is handling a certain level of data fetching, it needs to have integrations with specific frameworks that can handle this in the way that it intends to do so. Since SSR and SSG is becoming the norm for react, my thoughts on a refactor would be some or all of the following:
If I can find time, I will try to experiment with some of these concepts, but would be interested in feedback from @cookpete on these thoughts also. |
@panzacoder I appreciate the thought you’ve put into this. Unfortunately I think we’re at the point where a proper solution is simply not worth the time and effort it would take – you might as well rewrite the library from scratch. I have been gradually rewriting this with typescript, hooks, storybook, etc, but getting time to work on it is tricky. There is no way to avoid using Some ideas:
|
For anything not Next.... im presuming using loadable components would work a treat. |
works for me. thanks ! ;) |
Having the same issue with next13 but this seems to be indeed the best solution right now. I've also took a look at the Next13 documentation: https://nextjs.org/docs/app/building-your-application/optimizing/lazy-loading |
It is not possible to be used on SSR(what I think it is intended for), so we have to use it on the client side:
Otherwise, we will have the next issue:
|
# Why <!-- Please describe the motivation for this PR, and link to relevant GitHub issues, forums posts, or feature requests. --> After upgrading `react-player` dependency to the latest, the following hydration issue started occurring where we use `Video` component. ![image](https://github.com/expo/expo/assets/10234615/62b3b8f9-7788-468a-a0a1-2a4793236060) I found a similar open issue in React Player GitHub repo (cookpete/react-player#1474) where it states that the library is not SSR friendly and this issue happens with React 18. # How <!-- How did you build this feature or fix this bug and why? --> Apply the workaround suggested [here](cookpete/react-player#1474 (comment)) which implies to lazy load the player using Next.js dynamic imports. # Test Plan <!-- Please describe how you tested this change and how a reviewer could reproduce your test, especially if this PR does not include automated tests! If possible, please also provide terminal output and/or screenshots demonstrating your test/reproduction. --> Tested this locally and there doesn't seem to be the hydration error on docs where we use the `Video` component. To test it locally, run docs locally and go to: http://localhost:3002/develop/development-builds/introduction/ # Checklist <!-- Please check the appropriate items below if they apply to your diff. This is required for changes to Expo modules. --> - [ ] Documentation is up to date to reflect these changes (eg: https://docs.expo.dev and README.md). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin).
# Why <!-- Please describe the motivation for this PR, and link to relevant GitHub issues, forums posts, or feature requests. --> After upgrading `react-player` dependency to the latest, the following hydration issue started occurring where we use `Video` component. ![image](https://github.com/expo/expo/assets/10234615/62b3b8f9-7788-468a-a0a1-2a4793236060) I found a similar open issue in React Player GitHub repo (cookpete/react-player#1474) where it states that the library is not SSR friendly and this issue happens with React 18. # How <!-- How did you build this feature or fix this bug and why? --> Apply the workaround suggested [here](cookpete/react-player#1474 (comment)) which implies to lazy load the player using Next.js dynamic imports. # Test Plan <!-- Please describe how you tested this change and how a reviewer could reproduce your test, especially if this PR does not include automated tests! If possible, please also provide terminal output and/or screenshots demonstrating your test/reproduction. --> Tested this locally and there doesn't seem to be the hydration error on docs where we use the `Video` component. To test it locally, run docs locally and go to: http://localhost:3002/develop/development-builds/introduction/ # Checklist <!-- Please check the appropriate items below if they apply to your diff. This is required for changes to Expo modules. --> - [ ] Documentation is up to date to reflect these changes (eg: https://docs.expo.dev and README.md). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin).
'use client'
import { useState, useEffect } from 'react'
export default function App() {
const [isClient, setIsClient] = useState(false)
useEffect(() => {
setIsClient(true)
}, [])
return {isClient ? <ReactPlayer /> : <p>The video player cannot render on the server side</p>}
} |
from #1428, if above doesn't work, try |
we have released a canary version that should resolve this issue in many cases. |
duplicate of #1428 |
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
react-player
and follow typical implementation instructions.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
andreact-dom
may need to be updated.<div>
inside<p>
will cause the error.<Suspense>
.Please check again these scenarios in the codebase. Thank you!
The text was updated successfully, but these errors were encountered: