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

224
Views
How to dynamically create a component tree in React based on an array that contains components?

I have a components array.

const components = [
  A,
  B,
  C,
]

Then I want to render this components like this:

return (
  <A>
    <B>
      <C>
      </C>
    </B>
  </A>
)

But I can add a new component or switch the order of the components. I want to render this component tree in the same order as the components array.

So how can I dynamically create a component tree based on the components array?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

at least one

Write a recursive function, nest. At least one component is required -

const nest = ([T, ...more]) =>
  more.length == 0
    ? <T />
    : <T>{nest(more)}</T>

Use it in your components like this -

function MyComp(props) {
  return nest([A, B, C])
}

possibly zero

If you want the possibility to support an empty array, this is safest -

const nest = ([T, ...more]) =>
  T == null
    ? null
    : <T>{nest(more)}</T>
nest([]) // => null
nest([A,B,C]) // => <A><B><C/></B></A>

with props

If you want to supply props to the components, you can use a similar technique -

const nest = ([T, ...more], [p, ...props]) =>
  T == null || p == null
    ? null
    : <T {...p}>{nest(more, props)}</T>
nest([A,B,C], [{active: true}, {onClick: console.log}, {}])
<A active={true}>
  <B onClick={console.log}>
    <C />
  </B>
</A>

alternative

If props are needed, a better solution might be to colocate the props with the components using continuations -

const nest = ([T, ...more]) =>
  T == null
    ? null
    : T(nest(more))
nest([ 
  _ => <A active={true}>_</A>,
  _ => <B onClick={console.log}>_</B>,
  _ => <C/>
])
<A active={true}>
  <B onClick={console.log}>
    <C />
  </B>
</A>
about 4 years ago · Juan Pablo Isaza Report
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!