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

94
Visualizações
Load a react component on button onClick

I have the following code for a button in my ReactApp to open a new window with specific size:

<button
   type="button"
   style={{ height: '20px', width: '50px' }}
   className="btn btn-primary"
   onClick={() => {
   window.open(
   "",
   "Popup",
   "width=975, height=92, top=30"
       );
     }}
 />

I want to load another component when user clicks on the button. How can I achieve that? I have tried to load the component and then use it like the following, but didn't work:

import component from "./component"

    <button
       type="button"
       style={{ height: '20px', width: '50px' }}
       className="btn btn-primary"
       onClick={() => {
       window.open(
       "{<component />}",
       "Popup",
       "width=975, height=92, top=30"
           );
         }}
     />

What would be the best way to do this?

Here is my component:

export default function component(props) {
  return (
  
    <div className="container">
      <div className="cabinet"><img src={image} />
        <div className="devices"> <img src={image2} />
       
      </div>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Add a state in main page and pass it to component

 const [open, setOpen] = useState[false];

  const onClickHandle = () => {
    setOpen(true);
  };

  return (
    <>
      <button
        type="button"
        style={{ height: '20px', width: '50px' }}
        className="btn btn-primary"
        onClick={onClickHandle}
      />
      <component open={open} />
    </>
  );

Add a new props to control the component

export default function component(props) {
  const { open } = props;
  return (
    <>
      {open && (
        <div className="container">
          <div className="cabinet">
            <img src={image} />
            <div className="devices">
              {' '}
              <img src={image2} />
            </div>
          </div>
        </div>
      )}
    </>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Using state hooks and conditional rendering, you can load a react component on a button onClick.

const [visible, setVisible] = React.useState(false);

function click(e) {
  e.preventDefault();
  setVisible(true);
}

return (
  <div>
    <button onClick={(event) => {
       click(event)
    }} />
    
    {isVisible && (
      <YourComponent />
      )
    }
  </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