Skip to content

Commit

Permalink
fix hydration issue by preventing template creation during SSR (#1294)
Browse files Browse the repository at this point in the history
* fix hydration issue by preventing template creation during SSR

* fix lint

* revert galleryUiComponents change
  • Loading branch information
Galpittel authored Nov 20, 2024
1 parent a3567fc commit b281fbc
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions packages/gallery/src/components/item/media/GalleryUI.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { useGalleryUI } from '../../../context/GalleryContext';

type ComponentType = React.ComponentType<{ size: number }>;

interface GalleryComponents {
videoPlayButton: ComponentType;
rotateArrow: ComponentType;
}

const galleryUiComponents = {
videoPlayButton: React.lazy(() => import(/* webpackChunkName: "defaultPlayButton" */ './playButton')),
rotateArrow: React.lazy(() => import(/* webpackChunkName: "defaultRotateArrow" */ './rotateArrow')),
};

interface GalleryUIProps {
size: number;
type: 'videoPlayButton' | 'rotateArrow';
type: keyof GalleryComponents;
}

export const GalleryUI = ({ type, size }: GalleryUIProps): JSX.Element => {
let Component;

const [isMounted, setIsMounted] = useState(false);
const galleryUI = useGalleryUI();

useEffect(() => {
setIsMounted(true);
}, []);

if (!isMounted) {
return <></>;
}

if (typeof galleryUI?.[type] === 'function') {
return galleryUI[type](size);
} else if (galleryUiComponents[type]) {
Component = galleryUiComponents[type];
} else {
}

const Component = galleryUiComponents[type];

if (!Component) {
return <></>;
}

return (
<React.Suspense fallback={<></>}>
<Component size={size} />
Expand Down

0 comments on commit b281fbc

Please sign in to comment.