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

150
Visualizações
Passing value from a map function to a React component

I'm a little unsure how to take the value showBtn and pass it into my <ListComponent />

Here's the code:

  const listOptions = list.actionCollection
    .map((option) => {
      const showBtn = option.has(RENDER_BTN);

      // tried to assign showBtn to a value that could be passed to component below

      return option;
    });
{listOptions.map((option) => (
   <ListComponent option={option} key={option.key} />
))}

My check for showBtn is working fine (returning true or false) but I can't access showBtn outside of the map function. Obviously the map is checking through various values so some will be true & others false, so keeping the map syntax is important. Any suggestions would be appreciated!

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

0

You can return an array of booleans, indicating on which index your condition is true/false:

 const listOptions = list.actionCollection
    .map((option) => ( //Implicit return
      const showBtn = option.has(RENDER_BTN);
    ));

listOptions will look something like: [true, false, false, true...]

And you can use this in your jsx:

{list.actionCollection.map((option, index) => (
   listOptions[index] && <ListComponent option={option} key={option.key} />
))}

Or, simply:

{list.actionCollection.map((option, index) => (
   option.has('RENDER_BTN') && <ListComponent option={option} key={option.key} />
))}
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