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

168
Visualizações
React text dropdown toggle (toggles all)

I am trying to make a small text dropdown. When you click on the heading text, the p tag will show. It is working, but.. it is toggling all of them, when I only click on of them. I have multiple "li" tags with text and the function. So I am not looping anything

const [isActive, setActive] = useState(false)

  const toggleText = () => {
    setActive(!isActive)
  } 


  <li>
              <h2 onClick={toggleText}>Lorem ipsum dolar sit amet</h2>

              {isActive && (
                <p>
                  Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ut
                  reprehenderit explicabo laudantium quas, minus recusandae
                  quibusdam dolorem dolorum possimus natus quod nam, et labore
                  iste eos? Ducimus optio dolor soluta!
                </p>
              )}
              <div onClick={toggleText} className='dropDown__plus'>
                {!isActive ? (
                  <img src={plusIcon} alt='Plus icon' />
                ) : (
                  <img src={minusIcon} alt='Minus Icon' />
                )}
              </div>
            </li>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on your question and the comments, you are reusing the isActive state across multiple elements in the same component, which means that each element does not have its own isActive state: they all share the same state globally.

You can create a child component that handles the state exclusively, while allowing it to receive contents in the form of props, e.g. for the heading and content:

import { useState } from "react";

export default function MyComponent(props) {
  const [isActive, setActive] = useState(false);

  const toggleText = () => {
    setActive(!isActive);
  };

  return (
    <li>
      <h2 onClick={toggleText}>{props.heading}</h2>

      {isActive && props.content}
      <button type="button" onClick={toggleText} className="dropDown__plus">
        {!isActive ? <>+</> : <>-</>}
      </button>
    </li>
  );
}

Then, in your original parent component it's just a matter of using <MyComponent> to handle all your <li> entries:

<ul>
  <MyComponent
    heading="Lorem ipsum dolor sit amet"
    content={<p>Some text here</p>}
  />
  <MyComponent
    heading="Foo bar"
    content={<p>More foo bar content</p>}
  />
  <MyComponent
    heading="Last header"
    content={<p>C'est la vie</p>}
  />
</ul>

You can see a proof-of-concept example here:

Edit aged-field-nj1z6

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