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

166
Views
Switch case return multiple component

I loop on an object and I would like to display a result based on the Object.entries.

However the loop stops at the first return.

How can I get and display in one time what the components return to me? In a variable maybe ? Thanks

export const ResultItem: React.FC<Props> = (props:Props) => {
  const search = props
  
  const provideItem = () => {

  for (const [key, value] of Object.entries(search.result))
  {
    switch(key) {
      case "companies": 
        return <SearchCompany result={search.result[key]}/>
      case "medias":
        return <SearchMedias result={search.result[key]}/>
      case "contracts":
        return <SearchContracts result={search.result[key]}/>
      case "contacts":
        return <SearchContacts result={search.result[key]}/>
      }
    }
  }
  return (<div>{provideItem()}</div>)
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since you're returning a component, the execution of the for loop ends on the first iteration. An easy fix would be to create an array of items and push components into it, then render them at once.

export const ResultItem: React.FC<Props> = (props:Props) => {
  const search = props
  
  const provideItem = () => {
    const items = []
    for (const [key, value] of Object.entries(search.result))
    {
        switch(key) {
          case "companies": 
            items.push(<SearchCompany result={search.result[key]}/>)
          case "medias":
            items.push(<SearchMedias result={search.result[key]}/>)
          case "contracts":
            items.push(<SearchContracts result={search.result[key]}/>)
          case "contacts":
            items.push(<SearchContacts result={search.result[key]}/>)
         }
     }
     return items
  }
  return (<div>{provideItem()}</div>)
}

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!