Add <ClientOnly>
#2852
Replies: 4 comments
-
My current use case is a code editor. I am rendering it wrapped in a |
Beta Was this translation helpful? Give feedback.
-
Maybe have a SSR fallback <ClientOnly fallback={<EditorSSRFallback />}>
<ActualEditor />
</ClientOnly> In the fallback you can render something useful (like a |
Beta Was this translation helpful? Give feedback.
-
Heck yes, and let hydrating = true;
function ClientOnly({ children, fallback = null }) {
let [hydrated, setHydrated] = useState(() => !hydrating);
useEffect(() => {
hydrating = false;
setHydrated(true);
}, []);
return hydrated ? children : fallback;
} I think I'd like to call it |
Beta Was this translation helpful? Give feedback.
-
I built this in my project as two parts, a useHydrated hook and a ClientOnly component. import { useEffect, useState, ReactNode } from "react";
let hydrating = true;
export function useHydrated() {
let [hydrated, setHydrated] = useState(() => !hydrating);
useEffect(function hydrate() {
hydrating = false;
setHydrated(true);
}, []);
return hydrated;
}
type Props = {
children: ReactNode;
fallback?: ReactNode;
};
export function ClientOnly({ children, fallback = null }: Props) {
return useHydrated() ? <>{children}</> : <>{fallback}</>;
} With the hook I could do things like change attributes of an element if it's hydrated, e.g. I disable a fieldset if it's not hydrated because the fieldset has checkboxes which POST using useSubmit and they don't work without JS, but I still want to render them server-side. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Lots of third party libs don't work with server rendering and we keep posting the same code in the discord to help out. Should probably just put it into Remix itself.
Beta Was this translation helpful? Give feedback.
All reactions