Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use react-player throws a Hydration error

Hi how are you? I'm trying to use react-player in my Next.js app without any luck.

This code

import ReactPlayer from "react-player";

const Home = () => {
return (
  <div>
    <p>Here is my video!!</p>
    <div className={s.wrapper}>
      <ReactPlayer src='https://www.youtube.com/watch?v=zohpogt56Bg' width="100%" 
       height="100%" className={s.player} />
    </div>
  </div>
)}

keeps throwing this error: Error: Hydration failed because the initial UI does not match what was rendered on the server.

Does anybody knows what is going on? thanks!

like image 805
Emiliano Avatar asked Nov 30 '25 07:11

Emiliano


2 Answers

You can change your import to utilize Next's dynamic import. If you are only using static site generation then this option should work for you. It does seem like a bandaid fix however, because I don't think it solves the root problem.

import dynamic from 'next/dynamic'
const ReactPlayer = dynamic(() => import("react-player"), { ssr: false });
like image 172
c-concat-p Avatar answered Dec 02 '25 21:12

c-concat-p


Have you tried this?

It's basically checking the window.

import ReactPlayer from "react-player";

const Home = () => {
const [hasWindow, setHasWindow] = useState(false);
  useEffect(() => {
    if (typeof window !== "undefined") {
      setHasWindow(true);
    }
  }, []);
return (
  <div>
    <p>Here is my video!!</p>
    <div className={s.wrapper}>
      {
        hasWindow && <ReactPlayer src='https://www.youtube.com/watch?v=zohpogt56Bg' 
        width="100%" 
        height="100%" className={s.player} />
    }
    </div>
  </div>
)}
like image 20
Yogesh Bangar Avatar answered Dec 02 '25 20:12

Yogesh Bangar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!