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

108
Visualizações
How to give hide/show feature to certain components when it is mapped in react-native

I am getting certain response from api:

[
    {   
        "name": "cds",
        "user_id": "1b625caf-9bea-4846-bdec-0cbcd76149cf"
    },
    {   
        "name": "asd",
        "user_id": "1b625caf-9bea-4846-bdec-0cbcd76149cf"
    },
    {
        "name": "qee",
        "user_id": "1b625caf-9bea-4846-bdec-0cbcd76149cf"
    },
    {
        "name": "cbb",
        "user_id": "1b625caf-9bea-4846-bdec-0cbcd76149cf"
    },
    {
        "name": "rty",
        "user_id": "1b625caf-9bea-4846-bdec-0cbcd76149cf"
    },
]

And I am using a component to show the details of this api:

<TouchableOpacity>
    <Text>Toggle</Text>
</TouchableOpacity>

{upcoming.map((item: any, key: any) => (
    <>
        <UpComingComponent
            name={item.name}
        />
    </>
))}

I am able to map the component successfully, But I need to have a feature, where initially user can see only two mapped UpComingComponent, and when user clicks on toggle, user should see all the mapped UpComingComponent, and when he again clicks on toggle, user show again only see two UpComingComponent.

I know how to have hide/show a component, but my problem is I have show two components initially, then toggle, show all the components, and again toggle, show only two components.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use Array.slice and combine it with a state variable for toggle:

const [toggle, setToggle] = useState(false)

<TouchableOpacity onPress={() => setToggle(t => !t)}>
    <Text>Toggle</Text>
</TouchableOpacity>

{toggle ? upcoming.map((item: any, key: any) => (
    <>
        <UpComingComponent
            name={item.name}
        />
    </>
)) : upcoming.slice(0,2).map() => ...}
about 4 years ago · Juan Pablo Isaza Relatório

0

You could filter the list for the first two items based on their index.

upcoming
.filter((item, index) => (!showAll ? index < 2 : true))
.map((item) => (
  <>
    <UpComingComponent name={item.name} />
  </>
));
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