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

144
Visualizações
How do I toggle the elements with its corresponding button by mapping through an array in ReactJS?
<div>
    {arrayOfDetails.map((deets, index) => (
            <div>
                <h1>{deets.firstName + ' ' + deets.lastName}</h1>
                <div>
                    <div id={index}>
                        {listOfGrades(deets.grades)}
                    </div>
                </div>
                <button type="button" data-toggle="button" aria-pressed="false" autoComplete="off">+</button>
            </div>
    ))}
</div>

I'm mapping through an array from a JSON. How do I get the button to toggle the display of the div containing listOfGrades with the ID?

So 20 buttons for 20 listOfGrades divs, and each button should toggle its own corresponding div.

Thanks!

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

0

add a property isActive to each data object.

for example :

setArrayOfDetails(dataComingFromServer.map(item=>({...item,isActive:true})))

create the callback for toggling active status:

const toggleActive=(index)=>{

 const newState=[...arrayOfDetails]
 newState[i].isActive=!newState[i].isActive
 setArrayOfDetails(newState)
}

now render the items that are active:

<div>
{arrayOfDetails.map((deets, index) => (
        <div>
            <h1>{deets.firstName + ' ' + deets.lastName}</h1>
             <div>
             {deets.isActive &&  <div id={index}>
                    {listOfGrades(deets.grades)}
                </div>}
            </div>
            <button type="button" data-toggle="button" aria-pressed="false" autoComplete="off" onClick={()=>toggleActive(index)}>+</button>
        </div>
))}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can add a property in your data which keeps track of the hidden state. Then on button click for a row, find the index and toggle hidden property for that row.

  const [deets, setDeets] = useState([]); // Set initial Value here with hidden property for each item in array

  const toggleElement = (idx) => {
    let newDeets = [...deets];
    deets[idx].hidden = !deets[idx].hidden;
    setDeets(newDeets);
  };

  return (
    <div>
      {arrayOfDetails.map((deets, index) => (
        <div>
          <h1>{deets.hidden ? "" : `${deets.firstName} ${deets.lastName}`}</h1>
          <div>
            {deets.hidden ? (
              ""
            ) : (
              <div id={index}>{listOfGrades(deets.grades)}</div>
            )}
          </div>
          <button
            type="button"
            data-toggle="button"
            aria-pressed="false"
            autoComplete="off"
            onClick={() => toggleElement(index)}
          >
            +
          </button>
        </div>
      ))}
    </div>
  );
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