Skip to content

Commit 69f4878

Browse files
committed
fixes
1 parent 7c54729 commit 69f4878

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

e2e/solid-start/basic/src/client.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ console.log("[client-entry]: using custom client entry in 'src/client.tsx'")
77

88
const router = await hydrateStart()
99

10-
hydrate(() => <StartClient router={router} />, document.body)
10+
hydrate(() => <StartClient router={router} />, document)

packages/solid-router/src/ClientOnly.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import * as Solid from 'solid-js'
2-
import { isServer } from 'solid-js/web'
32

43
export interface ClientOnlyProps {
54
/**
6-
* The children to render if the JS is loaded.
5+
* The children to render when the JS is loaded.
76
*/
87
children: Solid.JSX.Element
98
/**
@@ -30,9 +29,37 @@ export interface ClientOnlyProps {
3029
* ```
3130
*/
3231
export function ClientOnly(props: ClientOnlyProps) {
32+
const hydrated = useHydrated()
3333
return (
34-
<Solid.Show when={!isServer} fallback={props.fallback}>
34+
<Solid.Show when={hydrated()} fallback={props.fallback ?? null}>
3535
<>{props.children}</>
3636
</Solid.Show>
3737
)
3838
}
39+
40+
/**
41+
* Return a boolean indicating if the JS has been hydrated already.
42+
* When doing Server-Side Rendering, the result will always be false.
43+
* When doing Client-Side Rendering, the result will always be false on the
44+
* first render and true from then on. Even if a new component renders it will
45+
* always start with true.
46+
*
47+
* @example
48+
* ```tsx
49+
* // Disable a button that needs JS to work.
50+
* const hydrated = useHydrated()
51+
* return (
52+
* <button type="button" disabled={!hydrated()} onClick={doSomethingCustom}>
53+
* Click me
54+
* </button>
55+
* )
56+
* ```
57+
* @returns True if the JS has been hydrated already, false otherwise.
58+
*/
59+
function useHydrated(): Solid.Accessor<boolean> {
60+
const [hydrated, setHydrated] = Solid.createSignal(false)
61+
Solid.onMount(() => {
62+
setHydrated(true)
63+
})
64+
return hydrated
65+
}

packages/solid-router/src/Transitioner.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export function Transitioner() {
1515
select: ({ isLoading }) => isLoading,
1616
})
1717

18+
if (router.isServer) {
19+
return null
20+
}
21+
1822
const [isTransitioning, setIsTransitioning] = Solid.createSignal(false)
1923
// Track pending state changes
2024
const hasPendingMatches = useRouterState({

0 commit comments

Comments
 (0)