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

95
Visualizações
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 Respostas
Responde à pergunta

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 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