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

114
Visualizações
Hide other item when one is clicked without define any class in react accordion

I implement the accordion but now I want to show only one particular item at a time. I know there are plenty answers available in this platform regarding this same problem but I can't find any suitable solution. Actually, I don't want to add any class in the active item and don't want to do that with CSS. With pure JavaScript, how can I achieve this? Here is my code:

   const PerFAQ = ({ ques, ans }) => {
    const [isShown, setIsShown] = useState(false)
    return (
        <>
            <li>
                <a onClick={() => setIsShown(!isShown)}>{ques}
                    {
                        !isShown ? <BsChevronDown /> : <BsChevronUp />
                    }
                </a>
                {
                    isShown && <ul>
                        <li>
                            {ans}
                        </li>
                    </ul>
                }
            </li>
        </>
    )
}

Willing to share more code if needed.

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

0

You can do this by lifting the isShown state up.

For example:

const ListItem = ({visible, onExpand}) => {
   return(
      <div>
          <button onClick={onExpand}>Expand</button>
          {visible && <p>your content</p>}
      </div>
   );
}

const List = ({items}) => {
   const [visibleIdx, setVisibleIdx] = useState(-1);
   return (
       <div>
            <ListItem visible={visibleIdx == 0} onExpand={() => setVisibleIdx(0)} />
            <ListItem visible={visibleIdx == 1} onExpand={() => setVisibleIdx(1)} />
       </div>
   )
}

Edit: It might be unclear what I meant. The React components form a tree hierarchy. Each item has children. The state of a component is stored by the component itself. You can pass these components down to its children, but cannot pass it up, to its parents. If you need to access the state from the parent, the only solution is to lift it up. That means you need to move the state to the parent component, and that component needs to pass it down to its children.

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