Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClientOnly #1592

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/solid/src/render/flow.ts
Expand Up @@ -7,7 +7,8 @@ import {
Accessor,
Setter,
onCleanup,
MemoOptions
MemoOptions,
onMount
} from "../reactive/signal.js";
import { mapArray, indexArray } from "../reactive/array.js";
import { sharedConfig } from "./hydration.js";
Expand Down Expand Up @@ -258,3 +259,23 @@ export function ErrorBoundary(props: {
"_SOLID_DEV_" ? { name: "value" } : undefined
) as Accessor<JSX.Element>;
}

export function ClientOnly(props: {
children?: JSX.Element,
Copy link

@emdede emdede Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case where you'd want to use a self-closing <ClientOnly />?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe 🤷 🤣

}): JSX.Element {
if (sharedConfig.context) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc this is the way to check if the app is currently hydrating. This check is needed so that UI correction can be applied, otherwise we can just render the children directly (since the app is just in CSR)

const [flag, setFlag] = createSignal(false);

onMount(() => {
setFlag(true);
});

return createMemo(() => {
if (flag()) {
return createMemo(() => props.children);
}
return undefined;
});
}
return createMemo(() => props.children);
}
4 changes: 4 additions & 0 deletions packages/solid/src/server/rendering.ts
Expand Up @@ -280,6 +280,10 @@ export function ErrorBoundary(props: {
return { t: `<!e${id}>${resolveSSRNode(res)}<!/e${id}>` };
}

export function ClientOnly(props: { children?: JSX.Element }): JSX.Element {
return undefined;
}

// Suspense Context
export interface Resource<T> {
(): T | undefined;
Expand Down