Reputation: 47
I have two navigation buttons that utilize the tag of react-router-dom. The problem I am facing is when I shift from the first button to the second button, the first button still remains active.
In my situation, I have a runs
button and input
button
The runs
button is in active state, but when the input
button is clicked, the run button is still active
Here is my snippet showing the navigation tabs : import { NavLink } from "react-router-dom";
<div className = "navigation-tabs">
<NavLink to={`/tasks/${task}`} activeStyle={{ backgroundColor: '#0022ff', color: '#FFF' }}>
Runs
</NavLink>
<NavLink to={`/tasks/${task}/input`} activeStyle={{ backgroundColor: '#0022ff', color: '#FFF' }}>
Input
</NavLink>
</div>
Upvotes: 0
Views: 540
Reputation: 287
Maybe exact
property can fix this. Because your URL like "/task/5" and "/task/5/input", in this case, router check the first part of URL so this two URL starts with "/tasks/5" and this why your link always active
<NavLink to={`/tasks/${task}`} exact activeStyle={{ backgroundColor: '#0022ff', color: '#FFF' }}>
Runs
</NavLink>
Upvotes: 1
Reputation: 36
React router dom will treat /tasks/${task}
and /tasks/${task}/input
as the same path. To change this, add exact
as an attribute to your navlink tags as follows:
<div className = "navigation-tabs">
<NavLink exact to={`/tasks/${task}`} activeStyle={{ backgroundColor: '#0022ff', color: '#FFF' }}>
Runs
</NavLink>
<NavLink to={`/tasks/${task}/input`} activeStyle={{ backgroundColor: '#0022ff', color: '#FFF' }}>
Input
</NavLink>
</div>
Upvotes: 1