Skip to content

fix: support falsy itemKey values such as 0 and empty string - #75

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/item-key-zero
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/item-key-zero

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Dec 1, 2026 •

Copy link
Copy Markdown

itemKey is typed React.Key | ((item) => React.Key), so 0 and '' are valid property names, but the key lookup was (itemKey && item?.[itemKey]) ?? index. A falsy itemKey made the whole expression 0 or '', which ?? does not replace, so the item key silently became 0/'' instead of the item's value.

The lookup now checks itemKey !== undefined && itemKey !== null before reading the property, and still falls back to the index when the property is missing.

Before / after, from the new test (tuple data with itemKey={0}, keys read from OverflowContext):

keys seen by the items
before not ['k-0', 'k-1'] (test fails)
after ['k-0', 'k-1']

The same test covers itemKey="" with { '': 'e-0' } style data and asserts that no React key warning is logged.

Tests: npx rc-test passes (7 suites, 38 tests) and the new test fails on the original source. eslint src/ reports 0 errors (4 warnings, identical on the base branch); tsc --noEmit prints no errors.

Summary by CodeRabbit

  • 修复
    • 修正列表项键的处理:空字符串和数字 0 等有效键不再被错误地替换为索引。
    • 当指定的键对应的值为空时,仍回退使用索引。

@vercel

vercel Bot commented Dec 1, 2026

Copy link
Copy Markdown

@mokshupadhyay is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

getKey 现在将 null 和 undefined 视为无效的 itemKey,并在属性值为空时回退到索引。新增测试覆盖数值 0 和空字符串键。

Changes

itemKey 处理

Layer / File(s) Summary
键值提取与回退
src/Overflow.tsx, tests/index.spec.tsx
getKey 现在接受数值 0 和空字符串作为 itemKey,并在取出的属性值为 null 或 undefined 时回退到索引。新增测试通过 OverflowContext 验证传递的键,并检查没有控制台错误。

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

Change: Bug fix

Suggested reviewers: qdyanbing

Merge Risk: Minimal · up to 77fc3

This change makes itemKey values of 0 and an empty string work as keys instead of falling back to the index. The scope is small and the new tests cover both cases, so the merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: support falsy itemKey values such as 0 and empty string”准确描述了主要变更。变更内容是让 Overflow.getKey 正确处理 itemKey 为 0 和空字符串等假值,而不是回退到索引。摘要显示变更仅限于 src/Overflow.tsx 的 getKey 逻辑,以及新增针对 itemKey={0} 和 itemKey=…
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.
Full details: Title check

Explanation

标题“fix: support falsy itemKey values such as 0 and empty string”准确描述了主要变更。变更内容是让 Overflow.getKey 正确处理 itemKey 为 0 和空字符串等假值,而不是回退到索引。摘要显示变更仅限于 src/Overflow.tsx 的 getKey 逻辑,以及新增针对 itemKey={0} 和 itemKey="" 的测试。标题与变更集相符,且具体。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

兔子轻敲键盘,数值零留下。
空字符串也有自己的位置。
键值若为空,索引来接手。
测试静静检查每个传递的键。
兔耳一晃,为新逻辑庆祝。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c582ecbb-9aad-4624-9b03-aa16fe5f647f
Commits

Reviewing files that changed from the base of the PR and between f1c801c and 77fc32e.

Files selected for processing: 2

  • src/Overflow.tsx
  • tests/index.spec.tsx

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

@mokshupadhyay

Copy link
Copy Markdown
Author

Hi @yoyo837, a friendly ping: this is ready for review whenever you have time. Happy to adjust anything. Thanks!

This branch has not been deployed

No deployments
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