Skip to content

Commit

Permalink
fix(react): rename useIsMount & update useEffect to useIsomorphicLayo…
Browse files Browse the repository at this point in the history
…utEffect (#246)
  • Loading branch information
minsoo-web authored Oct 24, 2023
1 parent a746830 commit c1edca2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-shrimps-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/react": patch
---

fix(react): convert Suspense.CSROnly's useEffect into useIsomorphicLayoutEffect to prevent unnecessary layout shift
6 changes: 3 additions & 3 deletions packages/react/src/Suspense.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ComponentProps, ComponentType, ReactNode, SuspenseProps as ReactSuspenseProps } from 'react'
import { Suspense as ReactSuspense, createContext, useContext } from 'react'
import { useIsMounted } from './hooks'
import { useIsClient } from './hooks'
import type { PropsWithoutChildren } from './types'

export type SuspenseProps = ReactSuspenseProps
Expand All @@ -21,10 +21,10 @@ if (process.env.NODE_ENV !== 'production') {
DefaultSuspense.displayName = 'Suspense'
}
const CSROnlySuspense = (props: SuspenseProps) => {
const isMounted = useIsMounted()
const isClient = useIsClient()
const fallback = useFallbackWithContext(props.fallback)

return isMounted ? <ReactSuspense {...props} fallback={fallback} /> : <>{fallback}</>
return isClient ? <ReactSuspense {...props} fallback={fallback} /> : <>{fallback}</>
}
if (process.env.NODE_ENV !== 'production') {
CSROnlySuspense.displayName = 'Suspense.CSROnly'
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { useIsChanged } from './useIsChanged'
export { useIsMounted } from './useIsMounted'
export { useIsClient } from './useIsClient'
export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
export { useKey } from './useKey'
export { usePrevious } from './usePrevious'
Expand Down
12 changes: 12 additions & 0 deletions packages/react/src/hooks/useIsClient.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { renderHook } from '@testing-library/react'
import { useIsClient } from '.'

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

expect(isClient).toBe(true)
})
})
12 changes: 12 additions & 0 deletions packages/react/src/hooks/useIsClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState } from 'react'
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'

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

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

return isClient
}
12 changes: 0 additions & 12 deletions packages/react/src/hooks/useIsMounted.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/react/src/hooks/useIsMounted.ts

This file was deleted.

0 comments on commit c1edca2

Please sign in to comment.