Skip to content

Referencing key within React.Children.map *is* defined. #10432

@hamlim

Description

@hamlim

This isn't necessarily a bug or a feature, more so asking for why it is implemented as it currently is.
When inside of a component, this.key is undefined, however child.key is defined and has a value within React.Children.map. I realize these point to different instances, just demonstrating the difference.

Example:

class Example extends React.Component {
  render() {
    console.log(this.key); // undefined
    return (
      <div>
       {React.Children.map(this.props.children, child => {
          console.log(child.key); // defined
          return child;
       })}
      </div>
    )
  }
}

I expect child.key to not be readable/writeable within React.Children.map, however it is both which seems counterintuitive to the idea that this.key is undefined within the component.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions