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

feat(react): improve ClientOnly by useSyncExternalStore #1105

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cyan-tigers-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/react": minor
---

feat(react): improve ClientOnly by useSyncExternalStore
2 changes: 1 addition & 1 deletion packages/react/src/components/ClientOnly.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactNode } from 'react'
import { useIsClient } from '../hooks/useIsClient'
import { useIsClient } from '../hooks'

interface ClientOnlyProps {
children: ReactNode
Expand Down
52 changes: 45 additions & 7 deletions packages/react/src/hooks/useIsClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
import { useIsomorphicLayoutEffect } from '@suspensive/utils'
import { renderHook } from '@testing-library/react'
import { useIsClient } from '.'
import { useState } from 'react'
import { useIsClient } from './useIsClient'

describe('useIsClient', () => {
it('should return true when client side painting start', () => {
const {
result: { current: isClient },
} = renderHook(() => useIsClient())

expect(isClient).toBe(true)
it('should return true in client side rendering', () => {
const returnedFirst = renderHook(() => useIsClient())
expect(returnedFirst.result.current).toBe(true)
returnedFirst.unmount()
const returnedSecond = renderHook(() => useIsClient())
expect(returnedSecond.result.current).toBe(true)
})
it("'s comparison with legacy useIsClientOnly", () => {
// check CSR environment first
expect(typeof document !== 'undefined').toBe(true)
const mockUseIsClient = vi.fn(useIsClient)
const { unmount } = renderHook(() => mockUseIsClient())
expect(mockUseIsClient).toBeCalledTimes(1)
unmount()
renderHook(() => mockUseIsClient())
expect(mockUseIsClient).toBeCalledTimes(2)
})
it('improve legacy useIsClientOnly', () => {
// check CSR environment first
expect(typeof document !== 'undefined').toBe(true)
let chanceIsClientToBeFalse = false
/**
* @deprecated This is legacy useIsClientOnly
*/
const mockUseIsClientLegacy = vi.fn(() => {
const [isClient, setIsClient] = useState(false)
if (!isClient) {
// eslint-disable-next-line react-compiler/react-compiler
chanceIsClientToBeFalse = true
}
useIsomorphicLayoutEffect(() => {
setIsClient(true)
}, [])
return isClient
})
const { unmount } = renderHook(() => mockUseIsClientLegacy())
expect(mockUseIsClientLegacy).toBeCalledTimes(2)
expect(chanceIsClientToBeFalse).toBe(true)
unmount()
renderHook(() => mockUseIsClientLegacy())
expect(mockUseIsClientLegacy).toBeCalledTimes(4)
expect(chanceIsClientToBeFalse).toBe(true)
})
})
17 changes: 6 additions & 11 deletions packages/react/src/hooks/useIsClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { useIsomorphicLayoutEffect } from '@suspensive/utils'
import { useState } from 'react'
import { noop } from '@suspensive/utils'
import { useSyncExternalStore } from 'react'

export const useIsClient = () => {
const [isClient, setIsClient] = useState(false)

useIsomorphicLayoutEffect(() => {
setIsClient(true)
}, [])

return isClient
}
const emptySubscribe = () => noop
const getSnapshot = () => true
const getServerSnapshot = () => false
export const useIsClient = () => useSyncExternalStore(emptySubscribe, getSnapshot, getServerSnapshot)
Loading