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

188
Visualizações
Call multiple functions with onClick event in React.js

The only problem I am facing is, either one of them dont work when put together. Both work fine independently. When put together, I can see the routes changing but the active Item doesn't switch.

 {SidebarData.map((item, index)=>{
      return(
        <div className={selected===index?'menuItem active': 'menuItem'}
        key={index}
        onClick={()=>{setSelected(index); window.location.pathname= item.link;    }}
       
        >
          <item.icon/>
          <span>
            {item.heading}
          </span>
        </div>
      )
    })}


export const SidebarData = [
  {
    icon: UilEstate,
    heading: "Dashboard",
    link: "/Dashboard",
  },
  {
    icon: UilLaptop ,
    heading: "Exams",
    link: "/Exams",
  },
  {
    icon: UilFileCheck ,
    heading: "Results",
    link: "/Result",
  },
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Assigning a new value to window.location.pathname will cause the browser to navigate to the new URL and load it as a new page.

If that URL also serves up your React application, then it will be run from scratch and all existing state (including the result of running setSelected(index);) will be lost.

Use Next.js or Gatsby to handle routing with static page generation or server-side rendering. Use BrowserRouter for routing with your own fallback code or HashRouter for a pure single page application with no server-side fallback.

These all have methods for passing state data through the link, so you can avoid using setState as well.

(You could also use BrowserRouter and serve up the same content-less bootstrap HTML document for every URL, but that isn't an efficient approach).

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