fix: match React's Children key and traversal semantics #37
main fromrefs/pull/37/head·opened Summary#
Children.map / forEach / count / toArray didn't match React's behavior in several ways. Keys could collide or change when children were reordered, which caused wrong DOM reuse and lost component state. This rewrites the traversal to follow React's mapIntoArray scheme.
Behavior changes#
- Empty slots:
count,mapandforEachnow includenull,undefinedand booleans and keep their positions (for example,Children.count([null, false, 'x']) === 3).toArraystill drops them. - Key format: keys now follow React's scheme:
.separates nesting levels,:separates siblings, and a keyed child's key is$plus its escaped key, used instead of the index. Duplicate keys in different nested arrays now stay distinct. - Stable keys on reorder: a keyed child keeps the same key wherever it moves, so state is kept when children passed through
Children.mapare reordered. - Lone child: a single child gets the same key it would have as the first item of an array (
.$a). - Keys from
map: a key returned from the callback is combined with the source child's key instead of replacing it. If the callback returns the child unchanged, the key isn't doubled. Arrays returned frommapare flattened (empty slots dropped) and their keys get the source key as a prefix. - Escaping:
=and:in user keys are escaped (=0,=2), and/is escaped in keys used as prefixes, matching React. - Running index: the
forEach/mapindex counts up across nested arrays, including empty slots.
Tests#
The new file tests/children-traversal-regressions.test.tsx has 15 cases covering each point above. One of them renders a list: it edits a keyed item's state, reorders the list through Children.map, and checks that the edited state is still there.
Summary by CodeRabbit#
- Bug Fixes
- Improved handling of nested children in mapping and counting, including empty slots and nested arrays.
- Preserved stable, distinct keys for nested and mapped children, including when keyed children are reordered.
- Updated array conversion to consistently traverse nested children and omit empty results.
- 1
Thanks for the key and traversal work here. A local continuation is now rebased onto current main and keeps your key escaping and source/mapped-key handling. It has not merged yet, the measured size and runtime tradeoffs are still being reviewed.
AI review notes: The agent tested the local four-file continuation, not the unchanged PR head. It passes 2,127 tests with 91 skipped, types and build checks. All 33 retained browser contracts pass against the candidate and native React in Chromium, Firefox and WebKit, current main passes only two per engine. This includes real keyed state preservation as well as traversal helpers. The candidate adds at most 315 gzip bytes or 360 Brotli bytes per affected build, and six fixed size limits still fail. Bounded 200–500-element probes also show some map/toArray operations slower than native React. These costs remain explicit delivery gates, no budget allowance or shipped fix is claimed.
Thanks for the update. I'd be happy to help with the remaining size and map/toArray performance gates if that's useful.
Thanks, that would help. A smaller traversal that keeps the same child keys and behavior for nested arrays and iterables would be useful. If you try one, could you share before-and-after
mapandtoArraytimings on the same inputs?AI review notes: On a local port of this PR to current main, the biggest measured increase was 315 bytes gzip and 360 bytes Brotli. In the selected 200- and 500-element cases,
maptook 1.18 to 1.80 times as long as native React, andtoArraytook 0.84 to 2.23 times as long. Those are local measurements on the port, separate from CI on this PR.I found one more case worth keeping in the traversal. The local port can read a custom iterator getter twice. I have a local version that reads it once, so you don’t need to duplicate that work if you’re looking at size.
AI review notes: With a getter that returns a generator method on the first read and undefined afterward, native React 19.3.1 reads the method once and
Children.toArrayreturns['a', 'b']. The saved local port reads twice and throws. The new local probe matches native React and preserves the iterator method’s receiver.Thanks for information.
Sign in to join this conversation.
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Advanced
Run ID:
da000216-4b4b-4da0-aaec-caab4c427338📥 Commits
Reviewing files that changed from the base of the PR and between ae632f06d9ca785da78d57bc4d5a88e1f79b6c47 and 40c7a86e953d28eedf6c5a9cb4c004c641448b15.
📒 Files selected for processing (2)
packages/redact/src/react/children.tstests/children-traversal-regressions.test.tsxIncluded review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
📝 Walkthrough
Walkthrough#
The Children utilities now recursively traverse nested arrays for mapping, counting, and array conversion. The changes add React-style key composition and regression tests for empty children, nested keys, callback indices, and keyed component state.
Changes#
React Children Traversal
packages/redact/src/react/children.ts,tests/children-traversal-regressions.test.tsxnullfor empty slots, and composes keys from nested paths and escaped user keys. Tests cover callback inputs, nested keys, and key stability.packages/redact/src/react/children.ts,tests/children-traversal-regressions.test.tsxChildren.countrecursively counts nested entries, andChildren.toArrayuses recursive mapping. Tests cover empty slots, callback indices, and keyed component state after reordering.Priority: ⬇️ Low
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
40c7aThe change moves Children traversal, counting and key generation to React-style semantics and adds regression tests. No concrete merge-blocking risk was identified.
Security Architecture Review#
Security architecture risk: 🔵 Low · up to
40c7aThe change remains within child processing and does not show a new privileged execution path. However, callbacks now run for previously skipped empty slots, and downstream applications were unavailable to determine whether those additional invocations affect sensitive operations.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share