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

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

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