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

190
Vistas
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 Respuestas
Responde la pregunta

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