Files
bracket/frontend/src/components/utils/react.tsx
2024-01-17 16:31:38 +01:00

25 lines
645 B
TypeScript

import { Fragment, createElement, useEffect, useState } from 'react';
/** React component that renders its children client-side only / after first mount */
export const ClientOnly = ({ children }: any) => {
const hasMounted = useClientOnly();
if (!hasMounted) {
return null;
}
// eslint-disable-next-line react/no-children-prop
return createElement(Fragment, { children });
};
/** React hook that returns true if the component has mounted client-side */
export const useClientOnly = () => {
const [hasMounted, setHasMounted] = useState(false);
useEffect(() => {
setHasMounted(true);
}, []);
return hasMounted;
};