Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda