Skip to content

Commit aa4d3c8

Browse files
committed
Add clientComponent
1 parent 1c857f2 commit aa4d3c8

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

packages/solid-use/src/client-only.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Component, JSX } from 'solid-js';
12
import {
23
Show,
34
createComponent,
@@ -7,11 +8,10 @@ import {
78
onMount,
89
sharedConfig,
910
} from 'solid-js';
10-
import type { Component, JSX } from 'solid-js';
1111
import { isServer } from 'solid-js/web';
1212

1313
export const createClientSignal = isServer
14-
? (): (() => boolean) => () => true
14+
? (): (() => boolean) => () => false
1515
: (): (() => boolean) => {
1616
const [flag, setFlag] = createSignal(false);
1717

@@ -50,14 +50,10 @@ export function clientOnly<T extends Component<any>>(
5050
const Lazy = lazy(fn);
5151
return ((props: any) => {
5252
if (sharedConfig.context) {
53-
const [flag, setFlag] = createSignal(false);
54-
55-
onMount(() => {
56-
setFlag(true);
57-
});
53+
const isClient = createClientSignal();
5854

5955
return createMemo(() => {
60-
if (flag()) {
56+
if (isClient()) {
6157
return createComponent(Lazy, props);
6258
}
6359
return undefined;
@@ -66,3 +62,16 @@ export function clientOnly<T extends Component<any>>(
6662
return createComponent(Lazy, props);
6763
}) as unknown as T;
6864
}
65+
66+
export function clientComponent<T extends Component<any>>(Comp: T): T {
67+
return ((props: any) => {
68+
const isClient = createClientSignal();
69+
70+
return createMemo(() => {
71+
if (isClient()) {
72+
return createComponent(Comp, props);
73+
}
74+
return undefined;
75+
});
76+
}) as unknown as T;
77+
}

0 commit comments

Comments
 (0)