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

85
Vistas
React Boostrap NavDropdown.Item closes modal when NavBar is closed

Hello i'm trying to open a Modal upon NavDropdown.Item click that is inside a NavBar,

If I do something like this

import { useState } from "react";
import { rockPositions } from "../../constant/markers"
import { CloseButton, NavDropdown, Alert } from "react-bootstrap";
import Modal from 'react-modal/lib/components/Modal';
import { RouteBadge } from "../RouteBadge/RouteBadge";
import { RouteTable } from "../RouteTable/RouteTable";
import { modalStyle } from '../RockModalMarker/RockModalMarker.css.js';

const RockDropDownModal = (props) => {

    const [isOpen, setIsOpen] = useState(false);
    const route = props.route;
    const rock = rockPositions.find(rockPosition => rockPosition.name === route.rock);
    const index = props.rockIndex;

    const toggleModal = (e) => {
        setIsOpen(!isOpen);
    };

    if (!isOpen) {
        return (
            <NavDropdown.Item key={index} href="#rock" onClick={toggleModal}><span className={"rock-name-dropdown"}>{route.rock}</span>: {route.name}</NavDropdown.Item>
        )
    }
    return (
        <Modal key={index}
            defaultStyles={modalStyle}
            isOpen={isOpen}
            onRequestClose={toggleModal}
            centered
            ariaHideApp={false}
        >
            <CloseButton variant="white" onClick={toggleModal} />
            <Alert variant="primary" style={{ marginTop: "20px", backgroundColor: "#01579B", borderColor: "#0277BD" }}>
                <Alert.Heading style={{ color: "#FFF" }}><p className="routeName">{rock.name}</p></Alert.Heading>
            </Alert>
            <RouteBadge rock={rock} />
            <RouteTable rock={rock} />
        </Modal>
    )
} 

export { RockDropDownModal };

the modal opens fine but it's behind the NavBar and if I close the navbar the modal closes too.

I was trying to close the navbar and only show the modal but and I wrap the dropdown item like this

<Navbar.Toggle>
            <NavDropdown.Item key={index} href="#rock" onClick={toggleModal}><span className={"rock-name-dropdown"}>{route.rock}</span>: {route.name}</NavDropdown.Item>
</Navbar.Toggle>

it closes the Navbar but also the Modal.

In the screenshot here you can see the NavBar, when the selected item is clicked the Modal should open and the navbar close.

How can I prevent the Modal from closing?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If one of the main problems here is that the navbar overlaps the modal, have you tried setting the z-index of the modal to a greater value? This would set the modal above the navbar and upon closing the modal, it will also close the navbar as well.

In addition to this, try to play around with expanded property of Navbar. You could create a state inside Navbar

const [isExpanded, setIsExpanded] = useState(`default_boolean_value`)
<Navbar expanded={isExpanded}/>

where-in upon clicking a dropdown item inside the child component, you could set the state of the Navbar to expanded=false

here is the documentation, you can find expanded prop here https://react-bootstrap.github.io/components/navbar/

about 4 years ago · Santiago Trujillo 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