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

React Router activeStyle return [object object]

I’m learning React Router and is testing some codes. I found that NavLink activeStyle is not working. I’ve followed the instructions on StackOverflow, I still get [object object]. My code:

class Menu extends Component {
    render() {
        return (
            <div>
                <nav>
                    <p><NavLink to="/" activeStyle={{ backgroundColor: "white" }}>Home</NavLink></p>
                    <p><NavLink to="/Contact" activeStyle={{ backgroundColor: "white" }}>Contact</NavLink></p>
                </nav>

                <h1>Begin</h1>
                <Routes>
                    <Route exact path="/" element={<Home />} />
                    <Route exact path="/Contact" element={<Contact />} />
                </Routes >

                <h1>Middle</h1>
                <p><Link to="/">Home</Link></p>
                <p><Link to="/Contact">Contact</Link></p>

                <h1>End</h1>
            </div >
        );
    }
}
export default Menu;

What I see in Inspect is:

enter image description here

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

Thanks.

>Solution :

react-router-dom no longer supports activeStyle property on NavLink component

You should use style prop instead:

Example:

<NavLink
  to="/"
  style={({ isActive }) =>
    isActive ? { backgroundColor: "white" } : undefined
  }
>
  Home
</NavLink>
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