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

119
Vistas
How do I handle an uncommon behavior of buttons in the array?

Lets say, I have a navigation bar with a few buttons(Home, Products, Jobs, etc.). All of those, but, for example, logout button, are serving with only one purpose - to navigate a user. Logout button, besides this common purpose, also has to send a query to backend to clean user's cookies, for example. Also, in case of app expanding we should make an array of buttons and map them into the component. So it kind of looks like this:

const coolButtons = [{path: '/Profile', title: 'Profile'}, {path: '/', title: 'Sign out'...}]

const testComponent = () => {
  return (
    <div>
      {coolButtons.map((button) => (
        <Button data-path={button.path}>{button.title}</Button> 
      ))}
    </div>
  );
};

And then, here in the component, I also want to write a handler for all these buttons.

const handleClick = (e: React.MouseEvent<HTMLElement>) =>{
if(path)
  navigate(e.currentTarget.dataset.path)// navigate from useNavigate().
}

But how could I handle logout logic aswell? Obviously the first thought that comes to mind is just to add a conditional and check if the path === sign out path and if so do its part of logic:

const handleClick = async (e) => {
  const { path } = e.currentTarget.dataset;
  if (path) {
    if (path === '/signout') {
      await logout(); // for example
      navigate(path);
    }
    navigate(path);
  }
};

But doesnt this solution seem quite buggy? What else could I do here?What if we have 2-3 buttons with uncommon logic? Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I don't know how you build Button component, but I'd suggest you this way. In coolButtons object, you can create actions. For example,

const coolButtons = [{
  path: '/Profile',
  title: 'Profile'
}, {
  path: '/',
  title: 'Sign out',
  action: async () => {
     await logout();
  }
}]

And after that, you can call that action in handleClick

const handleClick = (e) => {
  const { path, action } = e.currentTarget.dataset;
  if (path) {
    action && action(); //if action is in your dataset, you just call it
    navigate(path);
  }
};
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