admin管理员组文章数量:1399778
I have ponent which accepts children. Let's say;
<Toolbar><div>C1</div><div>C2</div></Toolbar>
When I print children using {children}
inside of Toolbar
, I can see them. However, I need to add/manupilate some props, so I want to iterate over them (like other arrays.map). However, when I try to use children.map
I get following error.
Warning: React.createElement: type is invalid -- expected a string (for built-in
ponents) or a class/function (for posite ponents) but got: object.
How can I achive this?
Thank you!
Edit; I mean something like;
{children.map((Child,index)=> <Child {...newProps}/>)}
I have ponent which accepts children. Let's say;
<Toolbar><div>C1</div><div>C2</div></Toolbar>
When I print children using {children}
inside of Toolbar
, I can see them. However, I need to add/manupilate some props, so I want to iterate over them (like other arrays.map). However, when I try to use children.map
I get following error.
Warning: React.createElement: type is invalid -- expected a string (for built-in
ponents) or a class/function (for posite ponents) but got: object.
How can I achive this?
Thank you!
Edit; I mean something like;
{children.map((Child,index)=> <Child {...newProps}/>)}
Share
Improve this question
edited Dec 25, 2017 at 18:07
Shubham Khatri
282k58 gold badges431 silver badges411 bronze badges
asked Dec 24, 2017 at 6:57
MohamedMohamed
1,2713 gold badges15 silver badges37 bronze badges
1
- assuming children is an array, you'll also potentially want to add a key property to each item - but I would check that all your ponents are exported properly out of their file they are defined in, and importing it in properly. I would want to see how you defined that as well. – Spencer Bigum Commented Dec 24, 2017 at 7:12
1 Answer
Reset to default 10You would make use of React.Children.map
and then React.cloneElement
to return the children after adding new props
{React.Children.map(children, child => {
return React.cloneElement(child, {
...newProps
});
})}
本文标签: javascriptAdd custom props to thispropschildren in React componentStack Overflow
版权声明:本文标题:javascript - Add custom props to this.props.children in React component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744198125a2594836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论