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

362
Vistas
React.js modal not appearing as it should

I'm dealing with a weird issue concerning modals/portals in React.js.

The issue is that the modal is not appearing as a pop-up over the other elements of the screen. I've been going through documentation but I can't spot my mistake.

The code below is for the modal called "SignIn" as well as the "App" files.

SignIn.js

import React from "react";
import ReactDOM from "react-dom"

const SignIn =({message,isopen,onclose})=>{
  if(!isopen) return null;
  return ReactDOM.createPortal(
    <div className="modal">
      <h2>{message}</h2>
      <button onClick={onclose}>Close</button>
    </div>,
    document.body
  );
};

export default SignIn;

App.js

import SignIn from "./Components/Modal/SignIn";

const App = ()=>{

    const [currentPaneName, setCurrentPaneName]=useState("MainPane");
    const [modalOpen, setModalOpen]=useState(false);

    return(
        <React.Fragment>
            <Header onNewPaneSelected={updateCurrentPane} />
            <div>
                <SignIn 
                message="This is a test modal"
                isopen={modalOpen}
                onclose={()=>setModalOpen(false)}
                />
            </div>
            <main>
                <button onClick={()=>setModalOpen(true)}>Open Modal</button>
                <LoadPaneHandler />
            </main>
            {/* <Footer /> */}
        </React.Fragment>
    );
}

export default App;

I've omitted code that wasn't relevant so that's why things may seem missing.

The result of this when I run the website is this...enter image description here

The button to open the modal appears but when it is clicked, it opens the text and the button to close it at the bottom of the screen and not over the other contents like a modal should. All of the other UI elements are completely usable while this is happening.

As I said, I've gone over this issue quite a few times and I can't spot my mistake. Any pointers would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I discovered the cause. I thought that only the Javascript code was needed to create a functional modal. Once I added a CSS file that specified elements such as...

  • Position
  • Top / Bottom / Left / Right
  • Background-Color (with a transparency value)
  • etc...

The modal began to work exactly as it should! I'm not sure why I believed that it would work without this but I was clearly mistaken.

Here's an example of the CSS I used to make this work.

.modal {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgb(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

I've also included a picture of the modal at work. Please ignore the ugly colors, they're placeholder while I experiment.

enter image description here

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