Skip to content
Advertisement

Conditional link styling React

I want my nav bar to style the page title I’m in, im using React and Tailwind CSS, for example, just make the title yellow when im on the selected path.

My logic to achieve that would be this but isn’t working:

JavaScript

My rout code:

JavaScript

Nav bar code:

JavaScript

Advertisement

Answer

Well at the end the problem was the path variable which was undefined, and wasn’t able to match href, so the condition never met.

Solution: call the path from useRouter hook with parameter .asPath, this gives me back the parameter which i stored in my path variable.

Code:

JavaScript

Nav bar code:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement