From acbb90e9574856d7a3ee666882b4b6dca95de3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=88=98?= <57122180+minsoo-web@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:05:33 +0900 Subject: [PATCH] fix(react): rename useIsMount & update useEffect to useIsomorphicLayoutEffect (#246) --- .changeset/tame-shrimps-rescue.md | 5 +++++ packages/react/src/Suspense.tsx | 6 +++--- packages/react/src/hooks/index.ts | 2 +- packages/react/src/hooks/useIsClient.spec.ts | 12 ++++++++++++ packages/react/src/hooks/useIsClient.ts | 12 ++++++++++++ packages/react/src/hooks/useIsMounted.spec.ts | 12 ------------ packages/react/src/hooks/useIsMounted.ts | 11 ----------- 7 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 .changeset/tame-shrimps-rescue.md create mode 100644 packages/react/src/hooks/useIsClient.spec.ts create mode 100644 packages/react/src/hooks/useIsClient.ts delete mode 100644 packages/react/src/hooks/useIsMounted.spec.ts delete mode 100644 packages/react/src/hooks/useIsMounted.ts diff --git a/.changeset/tame-shrimps-rescue.md b/.changeset/tame-shrimps-rescue.md new file mode 100644 index 000000000..734ff90ae --- /dev/null +++ b/.changeset/tame-shrimps-rescue.md @@ -0,0 +1,5 @@ +--- +"@suspensive/react": patch +--- + +fix(react): convert Suspense.CSROnly's useEffect into useIsomorphicLayoutEffect to prevent unnecessary layout shift diff --git a/packages/react/src/Suspense.tsx b/packages/react/src/Suspense.tsx index 0a5ca8f10..9d1a998bb 100644 --- a/packages/react/src/Suspense.tsx +++ b/packages/react/src/Suspense.tsx @@ -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 @@ -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 ? : <>{fallback} + return isClient ? : <>{fallback} } if (process.env.NODE_ENV !== 'production') { CSROnlySuspense.displayName = 'Suspense.CSROnly' diff --git a/packages/react/src/hooks/index.ts b/packages/react/src/hooks/index.ts index 5e354cd50..825ac85ad 100644 --- a/packages/react/src/hooks/index.ts +++ b/packages/react/src/hooks/index.ts @@ -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' diff --git a/packages/react/src/hooks/useIsClient.spec.ts b/packages/react/src/hooks/useIsClient.spec.ts new file mode 100644 index 000000000..ce705580b --- /dev/null +++ b/packages/react/src/hooks/useIsClient.spec.ts @@ -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) + }) +}) diff --git a/packages/react/src/hooks/useIsClient.ts b/packages/react/src/hooks/useIsClient.ts new file mode 100644 index 000000000..3329a6390 --- /dev/null +++ b/packages/react/src/hooks/useIsClient.ts @@ -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 +} diff --git a/packages/react/src/hooks/useIsMounted.spec.ts b/packages/react/src/hooks/useIsMounted.spec.ts deleted file mode 100644 index 5954eaacb..000000000 --- a/packages/react/src/hooks/useIsMounted.spec.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { renderHook } from '@testing-library/react' -import { useIsMounted } from '.' - -describe('useIsMounted', () => { - it('should return true when component is mounted', () => { - const { - result: { current: isMounted }, - } = renderHook(() => useIsMounted()) - - expect(isMounted).toBe(true) - }) -}) diff --git a/packages/react/src/hooks/useIsMounted.ts b/packages/react/src/hooks/useIsMounted.ts deleted file mode 100644 index 3ce612fe5..000000000 --- a/packages/react/src/hooks/useIsMounted.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { useEffect, useState } from 'react' - -export const useIsMounted = () => { - const [isMounted, setIsMounted] = useState(false) - - useEffect(() => { - setIsMounted(true) - }, []) - - return isMounted -}