Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  20] [ 6]  / answers: 1 / hits: 82344  / 8 Years ago, wed, april 20, 2016, 12:00:00

I'm trying to figure out how to clone an existing element with additional props.



For reference:



this.mainContent = <Hello message=Hello world! />


I attempted to do something like



React.createElement(this.mainContent, Object.assign({}, 
this.mainContent.props, { anotherMessage: nice to meet ya! }));


but it's not working.



How would I accomplish this?


More From » reactjs

 Answers
9

You need to clone the element and add the additional props using React.cloneElement e.g:


const ClonedElementWithMoreProps = React.cloneElement(
this.mainContent,
{ anotherMessage: "nice to meet ya!" }
);
// now render the new cloned element?

[#62466] Tuesday, April 19, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinah

Total Points: 268
Total Questions: 113
Total Answers: 89

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;