Skip to content

Commit db490c5

Browse files
authored
fix: nextjs hydration error from react-player (#528)
1 parent a9cfa19 commit db490c5

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/components/ReactPlayer/ReactPlayer.tsx

Lines changed: 29 additions & 21 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,
@@ -102,6 +103,9 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
102103
const [started, setStarted] = useState(autoPlay);
103104
const [paused, setPaused] = useState<boolean>(false);
104105
const [ended, setEnded] = useState<boolean>(false);
106+
const [isMounted, setIsMounted] = React.useState(false);
107+
108+
useMount(() => setIsMounted(true));
105109

106110
const videoSrc = useMemo(() => checkYoutubeVideos(src), [src]);
107111

@@ -332,26 +336,30 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
332336
ref={ref}
333337
onClick={handleClick}
334338
>
335-
<ReactPlayer
336-
className={b('player')}
337-
url={videoSrc}
338-
muted={muted}
339-
controls={controls === MediaVideoControlsType.Default}
340-
height={currentHeight || '100%'}
341-
width={width || '100%'}
342-
light={previewImgUrl}
343-
playing={isPlaying}
344-
playIcon={playIcon}
345-
progressInterval={FPS}
346-
onClickPreview={handleClickPreview}
347-
onStart={onStart}
348-
onReady={setPlayerRef}
349-
onPlay={onPlay}
350-
onPause={onPause}
351-
onProgress={onProgress}
352-
onEnded={onEnded}
353-
/>
354-
{renderCustomBarControls(muted, playedPercent)}
339+
{isMounted ? (
340+
<Fragment>
341+
<ReactPlayer
342+
className={b('player')}
343+
url={videoSrc}
344+
muted={muted}
345+
controls={controls === MediaVideoControlsType.Default}
346+
height={currentHeight || '100%'}
347+
width={width || '100%'}
348+
light={previewImgUrl}
349+
playing={isPlaying}
350+
playIcon={playIcon}
351+
progressInterval={FPS}
352+
onClickPreview={handleClickPreview}
353+
onStart={onStart}
354+
onReady={setPlayerRef}
355+
onPlay={onPlay}
356+
onPause={onPause}
357+
onProgress={onProgress}
358+
onEnded={onEnded}
359+
/>
360+
{renderCustomBarControls(muted, playedPercent)}
361+
</Fragment>
362+
) : null}
355363
</div>
356364
);
357365
},

0 commit comments

Comments
 (0)