Skip to content
Advertisement

React.js implement menu [highlight active link]

The following React.js code renders a navbar with two links named ‘about’ and ‘project’. On page load the ‘about’ link is active and colored red. When the other link is clicked the state of the navbar is set to ‘project’, ‘about’ link style is set back, and ‘project’ is colored red.

I achieve this by attaching a click handler to both link tags, and set the state of active to the name of the event.target.innerHTML.

I’m new to react, and I feel this is a really verbose way of going about this. I am aware that there is an activeClassName prop you can pass to a react-router link, but I want a way that does not use it.

JavaScript

Advertisement

Answer

maybe slightly less verbose… in Pseudocode

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