Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda