Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
How to highlight active nav link on click with Styled components and react-router-dom v6

I m new to react and I want my hover style stay active while the current Page is open.I need to make a condition but don't know how

const StyledLi = styled.li`
      `list-style: none;
      display: flex;

      &:hover {
        background-color: rgba(84, 78, 114, 1);
        border-radius: 10px 0px 0px 10px;//enter code here
      }
        background-color: ${props => props.path ? "rgba(84, 78, 114, 1)" : "white"};
      border-radius: ${props => props.path ? "0px 0px 0px 10px" : "0px"};
    ; 

    const SidebarMenu = () => {
        const location = useLocation();
        let path;
        const toHandler = (route) => (path = route);
    
        return (
            <StyledAside>
                <StyledUl>
                    <StyledLi>
                          <NavLink style={navLinkStyles} to={toHandler("/dashboard/main")}>
                            {location.pathname === path ? <StyledIcon url={homePageYellowIcon}/> : <StyledIcon url={homePageIcon}/>}
                            <span>Главная</span> 
                        </NavLink>
                    </StyledLi>
        );
    };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can reference other styled components. Import the Navlink and create a styled-component from it so it can be referenced within the StyledLi component.

import { NavLink as BaseNavLink } from "react-router-dom";

const NavLink = styled(BaseNavLink)`
  display: flex;
  align-items: center;
  font-size: 18px;
  color: #fff;
  font-weight: 700;
  text-decoration: none;
  padding: 8px 3px;
  width: 100%;

  &.active {
    color: #f8dc2f;
  }
`;

Now also apply the hover style when the nested NavLink component has an active "active" classname.

const StyledLi = styled.li`
  list-style: none;
  display: flex;

  &:hover,
  ${NavLink}.active {
    background-color: rgba(84, 78, 114, 1);
    border-radius: 10px 0px 0px 10px;
  }
  background-color: ${(props) =>
    props.path ? "rgba(84, 78, 114, 1)" : "white"};
  border-radius: ${(props) => (props.path ? "0px 0px 0px 10px" : "0px")};
`;

Edit how-to-highlight-active-nav-link-on-click-with-styled-components-and-react-route

enter image description here

Styling the NavLink

Here is the same thing but styling the NavLink.

const NavLink = styled(BaseNavLink)`
  display: flex;
  align-items: center;
  font-size: 18px;
  color: #888;
  font-weight: 700;
  text-decoration: none;
  padding: 8px 3px;
  width: 100%;

  &:hover,
  &.active {
    background-color: rgba(84, 78, 114, 1);
    border-radius: 10px 0px 0px 10px;
  }

  &.active {
    color: #f8dc2f;
  }
`;

const StyledLi = styled.li`
  list-style: none;
  display: flex;
  background-color: ${(props) =>
    props.path ? "rgba(84, 78, 114, 1)" : "white"};
  border-radius: ${(props) => (props.path ? "0px 0px 0px 10px" : "0px")};
`;

Edit how-to-highlight-active-nav-link-on-click-with-styled-components-and-react-route (forked)

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!