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
});
})}