reactjs / reactjs/react.dev

[Documentation] Add information about `child.key` to `React.Children.map`

Open
#2,023 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

I just spent quite a while debugging a performance issue when reordering a large list of components, which were wrapped in React.Children.map.

The issue was the implementation of the wrapping component, which used the index as the key, which caused the entire list to re-render from scratch:

<div>
  {React.Children.map(props.children, (child, index) => (
    <div key={index} className='...'>{child}</div>
  ))}
</div>

The solution was to use child.key and pass it into the child-component, which then, in turn, made the memoization of the child components work as expected:

<div>
  {React.Children.map(props.children, (child) => (
    <div key={child.key} className='...'>{child}</div>
  ))}
</div>

I feel like this should be mentioned somewhere on the documentation page for React.Children.map, since I only figured out that this is possible at all after specifically searching for it and finding #10432.

I'm happy to update the docs in a PR if this gets the OK :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the React.Children.map section on the React API documentation page linked in the issue, and review the related discussion in issue #10432. Add clear information about child.key and its relevance when wrapping children, then verify that the documentation explains the intended usage and performance consideration.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.