Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Is it possible to add props to the nested component using React.cloneElement?

Consider these components:

const Action = (props) => {
    return <div>Action</div>
}

const SpecialAction = (props) => {
    return <Action
        someProp={'someValue'}
    />
}

Now I'm being given an array of components, that are either Action or SpecialAction. And I want to extend them using React.cloneElement.

If I use this code:

const clonedActions = React.Children.toArray(actions)
.map(React.cloneElement(action, {
    additionalProp: 'additional prop value'
})

It won't affect Action components that are nested inside SpecialAction components.

I know I can refactor SpecialAction to pass the rest of the props:

const SpecialAction = ({specialProp1, specialProp2, ...rest}) => {
    return <Action
        {...rest}
        someProp={'someValue'}
    />
}

But I wonder if there is a way for me to not do that? Can I traverse the component hierarchy and add additional props to every Action component that I find?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!