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

React 18 + Suspense + useDeferredValue #1

Draft
wants to merge 4 commits into
base: before
Choose a base branch
from
Draft

React 18 + Suspense + useDeferredValue #1

wants to merge 4 commits into from

Conversation

dtinth
Copy link
Owner

@dtinth dtinth commented Feb 17, 2022

  • Upgraded to React 18 (has out-of-the-box support in Next.js)

  • Updated the code to use Suspense API (which already works in React 17)

  • Used useDeferredValue to remove the loading spinner when changing thread (this is new in React 18)

    • displayedMid = useDeferredValue(targetMid) lets MessageView render stale content instead of immediately switching to fallback.

    • To check if the currently displayed message is stale, we check that the currently displayed mid lags behind the target: targetMid !== displayedMid

  • Note that the documentation about the timeoutMs argument has been removed from both useDeferredValue and startTransition since August 2020. For rationale see: Disable timeoutMs argument facebook/react#19703

    • To defer displaying the loading spinner, the targetMid !== displayedMid can be used to check if content is stale, and loading spinner can be displayed with a delay by using CSS transition delays.

CodeSandbox: https://codesandbox.io/s/github/dtinth/2022-02-09-removing-loading-spinners

@dtinth dtinth changed the title React 18 + Suspense React 18 + Suspense + useDeferredValue Feb 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant