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

122
Visualizações
Ion menu side navbar is getting closed if I click on any ionlist's ionitem

here is my code

Menu.tsx

const Menu = () => {
  const showCat = () => {
    let res = Array(5)
      .fill(0)
      .map((_, i) => (
        <IonItem key={i}>
          <IonLabel>Cat{i}</IonLabel>
        </IonItem>
      ));
    return <IonList>{res}</IonList>;
  };
  return (
    <>
      <IonHeader>
        <IonToolbar>
          <IonTitle>Meme11</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent>
        <IonList>
          <IonMenuToggle auto-hide="false">
            <IonItem button routerLink={"/home"} routerDirection="none">
              <IonLabel>Home</IonLabel>
            </IonItem>
            <IonItem button routerLink={"/about"} routerDirection="none">
              <IonLabel>About</IonLabel>
            </IonItem>
            <IonItem
              onClick={() => {
                showCat();
              }}
            >
              <IonLabel>Categories</IonLabel>
            </IonItem>
          </IonMenuToggle>
        </IonList>
      </IonContent>
    </>
  );
};

App.tsx

const App: React.FC = () => (
  <IonApp>
    <IonSplitPane contentId="menu">
      <IonMenu class="custom" contentId="menu">
        <Menu />
      </IonMenu>
      <IonReactRouter>
        <IonRouterOutlet id="menu">
          <Route exact path="/home">
            <Home />
          </Route>
          <Route exact path="/">
            <Redirect to="/home" />
          </Route>
          <Route exact path="/about">
            <About />
          </Route>
        </IonRouterOutlet>
      </IonReactRouter>
    </IonSplitPane>
  </IonApp>
);

In the above code, If I click on any Item of Ion List, the sidenav is getting closed(toggled). And I don't want it to close only if i click outside the sidenav or on ionmenubutton, it should close. If I click on the ionitem 'categories', then showCat function should get called and it should return nested ionlist.

Thank You

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would try to stop propagation.

  <IonItem
          onClick={(e) => {
            e.stopPropagation();
            showCat();
          }}
        >

See more here: https://reactjs.org/docs/handling-events.html

UPDATE:

In order to make the sidebar stay just remove the ion menu toggle, like this:

 <IonContent>
    <IonList>
      
        <IonItem button routerLink={"/home"} routerDirection="none">
          <IonLabel>Home</IonLabel>
        </IonItem>
        <IonItem button routerLink={"/about"} routerDirection="none">
          <IonLabel>About</IonLabel>
        </IonItem>

        <IonItem
          onClick={(e) => {
            e.stopPropagation();
            showCat();
          }}
        >
          <IonLabel>Categories</IonLabel>
        </IonItem>

    </IonList>
  </IonContent>
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