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

308
Visualizações
Navbar logic - assigning a class to a button when navigating to a user route except for a specific user

I am trying to create a navbar with buttons that gain an 'activeTab' class if the browser route meets specific criteria for each button. For one of the buttons which takes a user to their profile, the activeTab class is added to the button when the browser is at the user page for the logged in user - code shown below:

className={
  location.pathname === "/userDetails" ||
  location.pathname === `/users/${userInfo.username}`
    ? "activeTab"
    : null
}

The above works well. What is not working is trying to implement logic which makes another button the activeTab when the browser navigates to a user page which is NOT the logged in user. The logic I have in mind is something like this:

className={
  location.pathname === "/exploreUsers" ||
  location.pathname === `/users/${!userInfo.username}`
    ? "activeTab"
    : null
  }

or this:

className={
  location.pathname === "/exploreUsers" ||
  location.pathname === `/users/${*}`
    ? "activeTab"
    : null
}

But I can't seem to get it to work. Any suggestions on the implementation here?

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

0

I think the issue lies with your !userInfo.username logic. That is returning a false or true value, and is not doing any comparison to the logged in user. So what that template literal is evaluating to is:

"users/false"

Instead, I think you could change the logic to do a comparison of the current user vs. the pathname. This will involve manipulating the pathname string to only look at the username portion.

ex:

className = {
  location.pathname === "/exploreUsers" ||
  location.pathname.split('/')[2] !== userInfo.username
    ? "activeTab"
    : null
}

I'm splitting the pathname string on the / character, which will return an array of strings that in between the slashes.

ex:

"/users/bobby".split('/')  // => ['', 'users', 'bobby']

Then I call the 2nd index of that array (the 3rd value), which will be the username bobby. From there I test that the current user does not equal the pathname user, and add the class accordingly.

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