Skip to content

fix: preserve non-element children when forwarding refs - #43

Open
nrps9909 wants to merge 1 commit into
react-component:masterfrom
nrps9909:codex/preserve-non-element-children-with-ref
Open

fix: preserve non-element children when forwarding refs#43
nrps9909 wants to merge 1 commit into
react-component:masterfrom
nrps9909:codex/preserve-non-element-children-with-ref

Conversation

@nrps9909

@nrps9909 nrps9909 commented Aug 27, 2026

Copy link
Copy Markdown

Summary

  • avoid cloning string and numeric ReactNode children when a Portal ref is present
  • use supportNodeRef to forward refs only to nodes that can actually accept them
  • add regression coverage for text and numeric children

Problem

PortalProps.children accepts React.ReactNode, but the ref path unconditionally called React.cloneElement. Passing a string or number together with a Portal ref therefore threw an invalid-element error instead of rendering the child.

Verification

  • reproduced both failures on exact base 4f29120a6403b4ac08ad21914107888aad90df9d
  • full client suite: 25/25 tests, 3/3 snapshots
  • SSR: 1/1 test
  • TypeScript: passed
  • ESLint: 0 errors (4 pre-existing hook warnings)
  • compile: ESM, CJS, and declarations passed
  • merged this branch with open PR feat: add CSP nonce support to Portal #35 locally: conflict-free; combined suite passed 32/32 client tests, SSR, and TypeScript

Open PR #35 touches the same files for CSP nonce support but a different region. Open PR #9 touches another part of Portal.tsx; neither implements this behavior.

AI assistance disclosure: Codex was used to trace the ref path, check open-PR overlap, draft tests, and run verification. The reported base failures and all passing results were reproduced locally.

Summary by CodeRabbit

  • Bug 修复

    • 优化 Portal 对子节点引用支持情况的判断。
    • 当子节点为文本或数字等不可引用内容时,仍可正常渲染,并正确返回空引用。
  • 测试

    • 增加对文本和数字子节点及 Portal 引用行为的测试覆盖。

@coderabbitai

coderabbitai Bot commented Aug 27, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7f1012c9-9286-4c68-80f2-91119e18544b

📥 Commits

Reviewing files that changed from the base of the PR and between 4f29120 and 3d7cd6f.

📒 Files selected for processing (2)
  • src/Portal.tsx
  • tests/index.test.tsx

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


Walkthrough

Portal 改用 supportNodeRef 判断子节点是否支持 ref。只有支持 ref 的子节点才会接收合并后的 ref。测试覆盖文本和数字子节点。

Changes

Portal ref 支持

Layer / File(s) Summary
ref 检测与非元素子节点验证
src/Portal.tsx, tests/index.test.tsx
Portal 使用 supportNodeRef 保存子节点的 ref 支持状态。仅当子节点支持 ref 时,Portal 才注入合并后的 ref。测试验证文本和数字子节点正常渲染,且 Portal ref 为 null

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 3d7cd

This localized fix preserves text and numeric Portal children when refs are supplied, with the reported client, SSR, TypeScript, lint, compile, and combined-suite checks passing; no actionable merge-blocking risk remains beyond normal checks and review.

Poem

小兔检查 ref,
文本数字都通过。
不支持的节点,
安静保持 null。
Portal 轻轻渲染,
测试萝卜香。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了主要变更:修复在转发 ref 时保留非元素子节点的问题。标题简洁、明确,并与代码和测试变更一致。
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant