question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Active Link Highlight

See original GitHub issue

Hello

How can I highlight a link that has been clicked. For example the supersimple code below will highlight link named ‘about’ in blue, for a second, after which it flickers back to normal navbar. Albeit I been able to in many ways custom style your navbar I can not for all the tea in the world figure out how to highlight an active link and keep it highlighted. For your info, I am using BrowserRouter, but not sure if that has any relevance…

Simple code (the blue highlight flickers when you click but then navbar link goes back to normal look and feel)

export default class Example extends React.Component {
  constructor(props) {
    super(props);

    this.toggle = this.toggle.bind(this);
    this.state = {
      isOpen: false,
    };
  }

  toggle() {
    this.setState({
      isOpen: !this.state.isOpen
    });
  }

  onClick() {
    this.setState({
      bgColor:'blue'
    })
  }

  render() {

    return (
      <Navbar color="light" light expand="sm">
        <NavbarBrand href="/">BLEBB BLEBB</NavbarBrand>
        <NavbarToggler onClick={this.toggle}/>
        <Collapse isOpen={this.state.isOpen} navbar>
          <Nav className="ml-auto" navbar>
            <NavItem className="navLink">
              <NavLink href="/" onClick={this.onClick.bind(this)} style={{backgroundColor:this.state.bgColor}}>About</NavLink>
            </NavItem>
            <NavItem className="navLink">
              <NavLink href="/work">Work</NavLink>
            </NavItem>
            <NavItem className="navLink">
              <NavLink href="/contact">Contact</NavLink>
            </NavItem>
          </Nav>
        </Collapse>
      </Navbar>
    );
  }

Thanks in Advance, fellow tea drinkers

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
thorstenssoncommented, Apr 16, 2018

Hello,

thanks for your answer, I had to interpolate the solution slightly. When I tried the code on the link in your reply, even if I copied and pasted the class, I recieved that activeClassName is not supported.

However, the below worked, and my menu now highlights nicely. That being sad I have little idea at this point what tag={} does. Could someone kindly explain what it does? After explaining that, you can close this as I now got it working. Unless, my solution is errorneous in some way!

import React from 'react';
import {
  Collapse,
  Navbar,
  NavbarToggler,
  NavbarBrand,
  Nav,
  NavItem,
  NavLink,
  UncontrolledDropdown,
  DropdownToggle,
  DropdownMenu,
  DropdownItem
} from 'reactstrap';
import './nav.css';
import { NavLink as RRNavLink } from 'react-router-dom';

export default class Example extends React.Component {
  constructor(props) {
    super(props);
    this.toggle = this.toggle.bind(this);
    this.state = {
      isOpen: false,
    };
  }

  toggle() {
    this.setState({
      isOpen: !this.state.isOpen
    });
  }

  render() {
    return (
      <Navbar color="light" light expand="sm">
        <NavbarBrand href="/">COOKIE MONSTER LIVES</NavbarBrand>
        <NavbarToggler onClick={this.toggle}/>
        <Collapse isOpen={this.state.isOpen} navbar>
          <Nav className="ml-auto" navbar>
            <NavItem className="navItem">
              <NavLink tag={RRNavLink} className="nav-link" to="/" activeClassName="active" exact path="/">About</NavLink>
            </NavItem>
            <NavItem className="navItem">
              <NavLink tag={RRNavLink} className="nav-link" to="/work" activeClassName="active">Work</NavLink>
            </NavItem>
            <NavItem className="navItem">
              <NavLink tag={RRNavLink} className="nav-link" to="/contact" activeClassName="active">Contact</NavLink>
            </NavItem>
          </Nav>
        </Collapse>
      </Navbar>
    );
  }
}

2reactions
thorstenssoncommented, Apr 16, 2018

Thanks, got it all working now! Moving on!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to highlight active links - html - Stack Overflow
I want to have clicked or active links turn purple so that one can tell which link is active. I tried a couple...
Read more >
how to highlight active nav-item? - Material Design for Bootstrap
A beginner question... How do I highlight the active nav-link so that it is clearly visible to the end user? In the example...
Read more >
Material UI - Navigation and Active Link Highlighting - YouTube
Check out Filestack - https://calcur.tech/filestack FREE Courses (100+ hours) - https://calcur.tech/all-in-ones Python Course ...
Read more >
how to make the active link highlighted - WordPress.org
Hi, I tried using custom css to style the menu bar, but I cant get the active page to highlighted with yellow. how...
Read more >
Highlight the active/current (pressed) button html by default
Hi All, this is for Service Portal. I have 2 buttons Active and Closed I want the Active button to be highlighted/selected by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found