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

394
Visualizações
How to hide/show a div in React?

I want to create a button to show and hide a div. In the first div (div1) I have a table and in the second div (div2) I have a chart. When a user clicks the button (with bar icon), should see the div2 (chart) and when the user clicks again it should return div1 (table). How can I do it?

tables.js

export function IncomeTables({firminfo, title, arr_number, financials, balance}) {
    ...
    ...

    return <Card>
    <div id="div1">
        <Table>
            <head>
               <tr>
                 <th colSpan="5">{title} <button style={{float:"right"}} className="btn btn-primary" ><FaChartBar/></button></th>
                 </tr>
                 ...
               </thead>
           ...
        </Table>
    </div>
    <div id="div2">
        <BarChart>
            ...
        </BarChart>
    </div>

    </Card>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is how you do it:

export default function App() {
  let [show, setShow] = React.useState(false);
  return (
    <div>
      {show ? <div>Hello</div> : <div>Another div</div>}
      <button
        onClick={() => {
          setShow(!show);
        }}
      >
        Click
      </button>
    </div>
  );
}

But when say instead of divs you render different types of components, when switching to another component, the previous one will be unmounted, and all state will be gone (due to reconciliation). In such cases you may want to store state in parent (e.g. App), so that state is not lost.

Or alternatively you may change display property of the div you want to show/hide, based on state variable; in that case state will not be lost, because you are not unmounting anything just changing a CSS property.

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