feat(utils): add getKey prop to <For> for stable list reconciliation#907
Merged
JoviDeCroock merged 2 commits intopreactjs:mainfrom Mar 27, 2026
Merged
feat(utils): add getKey prop to <For> for stable list reconciliation#907JoviDeCroock merged 2 commits intopreactjs:mainfrom
JoviDeCroock merged 2 commits intopreactjs:mainfrom
Conversation
The <For> component uses index-based keys on the internal <Item> wrapper,
causing incorrect DOM reuse when items are removed or reordered (the classic
React/Preact key problem).
Add an optional getKey prop to ForProps that lets consumers provide stable
identity keys derived from item data (e.g., getKey={item => item.id}).
When getKey is provided, its return value is used as the component key on
<Item>. When omitted, behavior is unchanged (index-based keys).
Changes:
- packages/react/utils: add getKey to ForProps, use it for <Item> key prop
- packages/preact/utils: same, also adds previously missing key prop on <Item>
- tests for both packages: removal, duplicates, reorder scenarios
🦋 Changeset detectedLatest commit: c128f77 The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for preact-signals-demo ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
JoviDeCroock
approved these changes
Mar 27, 2026
Merged
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.
Problem
The
<For>component uses index-based keys on the internal<Item>wrapper, causing incorrect DOM reuse when items are removed or reordered — the classic React/Preact key problem.Even when consumers set
key={item.id}on their rendered children, the<Item>wrapper'skey={index}overrides correct reconciliation.Solution
Add an optional
getKeyprop toForProps<T>:When
getKeyis provided, its return value is used as thekeyprop on the internal<Item>component, giving React/Preact stable identity for reconciliation.Usage
Changes
packages/react/utils: AddgetKeytoForProps, use it for<Item>key proppackages/preact/utils: Same changes + adds the previously missingkeyprop on<Item>Backward compatibility
getKeyis optional — omitting it preserves existing behavior (index-based keys).