Skip to content

[v6] [Bug]: React Router is too greedy. /routetypo matches /route instead of the * fallback #7972

Closed
@eakl

Description

@eakl

What version of React Router are you using?

6.0.0-beta.2

Steps to Reproduce

https://codesandbox.io/s/react-router-routing-j72xo?file=/src/App.js

Expected Behavior

Route path parameter is confusing

<Routes>
  <Route path="/" element={<p>Root</p>} />
  <Route path="home">
    <Route path="/" element={<p>Home</p>} />
    <Route path="*" element={<Navigate to={"."} />} />
  </Route>
  <Route path="*" element={<p>Not Found.</p>} />
</Routes>

/ should match Root
/home should look to its children and match Home
/hometypo should match Not Found
/home/wrongParam should match <Navigate ... />

Actual Behavior

/ matches Root (OK)
/home looks to its children and matches Home (OK)
/hometypo matched Home (NOT OK, RR is too greedy)
/home/wrongParam redirects to <Navigate /> (OK but the redirect path is inconsistent with <Route path />)

Referring to #7335, I agree that <Route path /> should implement a . for the root of the subcomponent instead of the forward slash.

<Route path="home">
  <Route path="/" element={<p>Home</p>} />
  <Route path="*" element={<Navigate to={"."} />} />
</Route>

When having <Route path /> and <Navigate to /> side by side, it becomes confusing to match the root of the parent route with a forward slash instead of a .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions