Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add color to Link in react router dom?

I am working with react router dom and trying to give style to active link.

The problem is when I give the active style to the url ‘/’ and when I click another Link, this active style does not leave. Why can it happen?

I mean I click the Link to the url ‘/’ , everything works ok, when I click another Link, the Link with url ‘/’ has active style, but I have already opened another page.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My code is below:

const SideBar = () => {
    
    const getStyle = (isActive:boolean) => {
        return isActive ? 'activePage' : 'inactive'
    }
    
    return (
        <div className="sidebar-overlay">
            <ul className="sidebar-content">
                <li>
                    <NavLink
                        className={({isActive}) => getStyle(isActive)}
                        to={'/'}>
                        <Home/>
                        Главная
                    </NavLink>
                </li>
                <li>
                    <NavLink
                        className={({isActive}) => getStyle(isActive)}
                        to={'/sections'}>
                        <Sections />
                        Отделы
                    </NavLink>
                </li>
                <li>
                    <NavLink
                        className={({isActive}) => getStyle(isActive)}
                        to={'/accounts'}>
                        <Accounts />
                        Учетные записи
                    </NavLink>
                </li>
            </ul>
        </div>
    )
};
function Application () {
    return (
        <div className="container">
            <div className="content">
                <BrowserRouter>
                    <Header/>
                    <SideBar/>
                    <Routes>
                            <Route path='/' element={<Main/>}/>
                            <Route path='/sections' element={<Sections/>}/>
                            <Route path='/accounts' element={<Accounts/>}/>
                    </Routes>
                </BrowserRouter>
            </div>
        </div>
    )
}
.activePage {
  color: white;
  border-radius: 10px;
  background-color: #007bff;
}

.inactive {
  color: black;
}

>Solution :

Try to define a state to represent the currently active page and change the class accordingly:

const SideBar = () => {
    const [active, setActive] = useState('')

  return (
    <div className='sidebar-overlay'>
      <ul className='sidebar-content'>
        <li>
          <NavLink className={active === '' ? 'activePage' : 'inactive'} onClick={() => setActive('')} to={'/'}>
            <Home />
            Главная
          </NavLink>
        </li>
        <li>
          <NavLink
            className={active === 'sections' ? 'activePage' : 'inactive'}
            onClick={() => setActive('sections')}
            to={'/sections'}
          >
            <Sections />
            Отделы
          </NavLink>
        </li>
        <li>
          <NavLink
            className={active === 'accounts' ? 'activePage' : 'inactive'}
            onClick={() => setActive('accounts')}
            to={'/accounts'}
          >
            <Accounts />
            Учетные записи
          </NavLink>
        </li>
      </ul>
    </div>
  );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading