fix(elements): keep attachment references stable during text input - #475
Open
ephraimduncan wants to merge 1 commit into
Open
fix(elements): keep attachment references stable during text input#475ephraimduncan wants to merge 1 commit into
ephraimduncan wants to merge 1 commit into
Conversation
The local attachments context cloned every attachment item on each
recompute (files.map((item) => ({ ...item, id: item.id }))), a pure
identity clone. Because the context memo also recomputes on every
keystroke in provider-backed mode, each text change allocated a new
object per attachment and defeated memoized attachment rows that
compare by reference, such as the memo'd AttachmentItem in the shipped
example.
Pass the files array through by reference instead. Unchanged items now
keep their identity while typing, so memoized chips skip re-renders on
text-only updates and still re-render when files are added or removed.
Two new tests cover provider-backed mode: one asserts item references
stay strictly equal across typing, and one asserts a React.memo chip
does not re-render on text input but does when a file is added. Both
fail before this change.
Contributor
|
@ephraimduncan is attempting to deploy a commit to the Vercel Team on Vercel. A member of the Team first needs to authorize it. |
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.
Memoized attachment rows re-rendered on each keystroke in provider-backed mode. The local attachments context made a new copy of each attachment object on each recompute, and each text change caused a recompute. Rows that compare attachments by reference, for example a
React.memochip, could not skip those renders.This change passes the
filesarray through by reference. Unchanged attachments keep their identity during text input, but rows still re-render when a file is added or removed. Two new tests make sure that references stay equal during text input and that a memoized chip re-renders only on file changes. The two tests are not successful before the change.