javascript - Why do keys change when children are used with React.Children.toArray? - Stack Overflow

If I have a react ponent with children and I call React.Children.toArray on these children, why does th

If I have a react ponent with children and I call React.Children.toArray on these children, why does the objects in the array have keys that are prepended with .$

var Child = React.createClass({
    render: function() {
        console.log(React.Children.toArray(this.props.children)[0].key);
        return <div>{this.props.children}</div>
    }
});

var Container = React.createClass({
    render: function() {   
        return <Child><div key={1}>1</div></Child>
    }
});

ReactDOM.render(<Container />,  document.getElementById('container'));

This logs .$1 to the console. Why is the key changed from 1 to .$1?

If I have a react ponent with children and I call React.Children.toArray on these children, why does the objects in the array have keys that are prepended with .$

var Child = React.createClass({
    render: function() {
        console.log(React.Children.toArray(this.props.children)[0].key);
        return <div>{this.props.children}</div>
    }
});

var Container = React.createClass({
    render: function() {   
        return <Child><div key={1}>1</div></Child>
    }
});

ReactDOM.render(<Container />,  document.getElementById('container'));

This logs .$1 to the console. Why is the key changed from 1 to .$1?

Share Improve this question edited Feb 13, 2021 at 7:36 Penny Liu 17.6k5 gold badges86 silver badges108 bronze badges asked Nov 18, 2015 at 22:08 SubHeroSubHero 1197 bronze badges 5
  • 1 It's just a React specific key that React internally uses in order to keep track of all the children in the virtual DOM tree. – Henrik Andersson Commented Nov 19, 2015 at 5:18
  • @HenrikAndersson Why does React.Children.map do the same? – tonix Commented Dec 24, 2019 at 11:44
  • 2 @tonix because React.Children is the same as this.props.children. – Henrik Andersson Commented Dec 25, 2019 at 17:05
  • @HenrikAndersson I am sorry, I have not expressed myself clearly. I meant, why does React.Children.map returns an array of children with their keys changed as React.Children.toArray? – tonix Commented Dec 26, 2019 at 9:02
  • Ah, I haven't found a source that explains the why behind the decision but if I were to hazard a guess then it's easier for the library to fix keys and rely on themselves than on product developers. If you want to dig into it yourself the investigation starts here: github./facebook/react/blob/… – Henrik Andersson Commented Dec 26, 2019 at 9:43
Add a ment  | 

1 Answer 1

Reset to default 7

See the note below https://facebook.github.io/react/docs/top-level-api.html#react.children.toarray

Note: React.Children.toArray() changes keys to preserve the semantics of nested arrays when flattening lists of children. That is, toArray prefixes each key in the returned array so that each element's key is scoped to the input array containing it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745453175a4628365.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信