Skip to content

Commit a648846

Browse files
authored
fix: nextjs hydration error from react-player (#526)
1 parent 74593e1 commit a648846

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/components/ReactPlayer/ReactPlayer.tsx

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, {
2+
Fragment,
23
useCallback,
34
useContext,
45
useEffect,
@@ -15,7 +16,7 @@ import ReactPlayer from 'react-player';
1516
import {MetrikaContext} from '../../context/metrikaContext';
1617
import {MobileContext} from '../../context/mobileContext';
1718
import {VideoContext} from '../../context/videoContext';
18-
import {useAnalytics} from '../../hooks';
19+
import {useAnalytics, useMount} from '../../hooks';
1920
import {PlayVideo} from '../../icons';
2021
import {
2122
AnalyticsEvent,
@@ -104,6 +105,9 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
104105
const [started, setStarted] = useState(autoPlay);
105106
const [paused, setPaused] = useState<boolean>(false);
106107
const [ended, setEnded] = useState<boolean>(false);
108+
const [isMounted, setIsMounted] = React.useState(false);
109+
110+
useMount(() => setIsMounted(true));
107111

108112
const videoSrc = useMemo(() => checkYoutubeVideos(src), [src]);
109113

@@ -337,27 +341,31 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
337341
ref={ref}
338342
onClick={handleClick}
339343
>
340-
<ReactPlayer
341-
className={b('player')}
342-
url={videoSrc}
343-
muted={muted}
344-
controls={controls === MediaVideoControlsType.Default}
345-
height={currentHeight || '100%'}
346-
width={width || '100%'}
347-
light={previewImgUrl}
348-
playing={isPlaying}
349-
playIcon={playIcon}
350-
progressInterval={FPS}
351-
onClickPreview={handleClickPreview}
352-
onStart={onStart}
353-
onReady={setPlayerRef}
354-
onPlay={onPlay}
355-
onPause={onPause}
356-
onProgress={onProgress}
357-
onEnded={onEnded}
358-
aria-label={ariaLabel}
359-
/>
360-
{renderCustomBarControls(muted, playedPercent)}
344+
{isMounted ? (
345+
<Fragment>
346+
<ReactPlayer
347+
className={b('player')}
348+
url={videoSrc}
349+
muted={muted}
350+
controls={controls === MediaVideoControlsType.Default}
351+
height={currentHeight || '100%'}
352+
width={width || '100%'}
353+
light={previewImgUrl}
354+
playing={isPlaying}
355+
playIcon={playIcon}
356+
progressInterval={FPS}
357+
onClickPreview={handleClickPreview}
358+
onStart={onStart}
359+
onReady={setPlayerRef}
360+
onPlay={onPlay}
361+
onPause={onPause}
362+
onProgress={onProgress}
363+
onEnded={onEnded}
364+
aria-label={ariaLabel}
365+
/>
366+
{renderCustomBarControls(muted, playedPercent)}
367+
</Fragment>
368+
) : null}
361369
</div>
362370
);
363371
},

0 commit comments

Comments
 (0)