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

203
Views
React - map over actual HTML children wrapped in HoC

I have a Component that renders all the children passed to it as list elements. For example:

// more or less the logic of my component
const ListifyChildren = (props) => {
  return <ul>{props.children.map(element => <li>{element}</li>)}</ul>
}

// example use
<ListifyChildren>
  <div>Some component</div>
  <div>Some other component</div>
</ListifyChildren>

Would produce

<ul class="listify">
  <li class="listify-list-item">Some component</li>
  <li class="listify-list-item">Some other component</li>
</ul>

But the problem is I want to be able to use HoC's that return a list of components and to treat those components as children of my actual list. For example:

const ReturnSomeStuff = () => {
 return [someArray].map(element => <div>{element}</div>
}

<ListifyChildren>
  <ReturnSomeStuff/>
</ListifyChildren>

//what I get:
<ul class="listify">
  <li class="listify-list-item">
   <div>something</div>
   <div>something</div>
   <div>something</div>
  </li>
</ul>

//what I want to get:
<ul class="listify">
   <li class="listify-list-item">something</li>
   <li class="listify-list-item">something</li>
   <li class="listify-list-item">something</li>
</ul>

How can I make sure that my component maps over actual html children, not the function calls passed to it?

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

0

You can use Fragment to do it: https://reactjs.org/docs/fragments.html

const ReturnSomeStuff = () => {
 return [someArray].map(element => <>{element}</>
}
about 4 years ago · Juan Pablo Isaza Report

0

I have found the answer: the key is referring to child elements via React.Children API rather than simple props.children - it returns the list of rendered elements.

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!