React 18 + Suspense + useDeferredValue #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)
letsMessageView
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 bothuseDeferredValue
andstartTransition
since August 2020. For rationale see: Disable timeoutMs argument facebook/react#19703targetMid !== 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