Optimize expanded image preview rerenders#2876
Draft
cursor[bot] wants to merge 4 commits into
Draft
Conversation
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What Changed
ExpandedImageDialogprop-mirroring Effect that copiedpreviewinto local state.keydownbrowser subscription into a nameduseExpandedImageKeyboardShortcutshook usinguseEffectEvent.Why
React Doctor flagged the old implementation as mirrored prop state via Effect. That caused an extra stale render whenever the parent selected a different preview. Keying the inner dialog resets navigation state from the selected preview without an Effect, while keeping arrow-key navigation local to the dialog.
UI Changes
React Scan recordings are included in this PR:
docs/perf/expanded-image-dialog-react-scan-before.webmdocs/perf/expanded-image-dialog-react-scan-after.webmThe same interaction, two parent preview switches, went from
5 totaldialog subtree commits before to3 totalcommits after. Metadata is in:docs/perf/expanded-image-dialog-react-scan-before.jsondocs/perf/expanded-image-dialog-react-scan-after.jsonChecklist
Verification:
npx -y react-doctor@latest . --project @t3tools/web --diff main --verbosebun fmtbun lintbun typecheckNote
Reduce rerenders in
ExpandedImageDialogby keying content on preview stateExpandedImageDialogwith a thin wrapper that renders a keyedExpandedImageDialogContentchild, forcing a remount whenpreview.index, image count, or image sources change via a newgetPreviewResetKeyhelper.indexOffsetinteger, derivingcurrentIndexas(preview.index + indexOffset) % images.length.useExpandedImageKeyboardShortcutshook usinguseEffectEventfor a stable listener.React.memo/useCallbackare removed.Macroscope summarized 64fcb1a.