Skip to content

[Bug]: NavLink is active for partial segment matches (react-router-dom v6.4) #9279

@fullsailor

Description

@fullsailor

What version of React Router are you using?

[email protected]

Steps to Reproduce

  1. Define sibling routes /user and /user-preferences
    <BrowserRouter>
      <Routes>
         <Route path="/" element={<Root />}>
            <Route index element={<p>Index</p>} />
            <Route path="user" element={<p>User</p>} />
            <Route path="user-preferences" element={<p>User Preferences</p>} />
         </Route>
      </Routes>
    </BrowserRouter>
  2. Show NavLinks to the routes
    const Root = () => (
       <>
          <NavLink to="user">Go to /user</NavLink>
          <NavLink to="user-preferences">Go to /user-preferences</NavLink>
       </>
    )
  3. Navigate to /user-preferences path

Code Sandbox

Expected Behavior

The link to /user-preferences should have the active class and the link to /user should not have the active class. This is the behavior in [email protected]

Actual Behavior

Both links have the active class.
Screen Shot 2022-09-16 at 10 18 05

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions