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

143
Views
Building a reusable component to map through different sets of data React

Just like the title says I'm passing down pokemon data and rickandmorty data. I also happen to be using the tailwind select menu for react thats pretty long. Is there a better way to do it than conditionally map through the data? I know I can do this

{pokemons ? (
            {pokemons?.map((pokemon, idx) => (
              **30 line long code for the select menu**
            ))}
         
        ) : (
            {rickAndMorty?.map((character, idx) => (
              **Another 30 long line code for the select menu**
            ))}
        )}

Is this the only way to do it or is there a cleaner way? Any help would be greatly appreciated. Thanks!

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

0

I suggest to try and separate any duplicated code out into some generic component, like:

const GenericSelectItem = (props)=>{
    return (<>{/* props.itemValues */}</>);
};

const GenericSelectList = (props)=>{
    const { selectItems } = props;
    return (<SelectList>
        { selectItems.map( selectItem => <GenericSelectItem selectItem={ selectItem } /> ) }
    </SelectList>);
};

const Example = (props)=>{
    const itemsToDisplay = pokemons || rickAndMorty;
    return (<>
        { !itemsToDisplay ? null : <GenericSelectList selectItems={ itemsToDisplay } /> }
    </>);
};

In case the SelectItems are very different, add specific components, like:

const PokemonItem = (props)=>{
    return (<GenericSelectItem>{/* pokemon specific variations */}</GenericSelectItem>);
};

const RickAndMortyItem = (props)=>{
    return (<GenericSelectItem>{/* rickAndMorty specific variations */}</GenericSelectItem>);
};
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!