Skip to content

[Bug]: Route id collision when using React.Fragment with data router #10111

@afn

Description

@afn

What version of React Router are you using?

6.8.1

Steps to Reproduce

  1. Use createRoutesFromElements() with React.Fragments — for example, modify the dev/examples/data-router example to wrap one of the routes inside a React.Fragment:
let router = createBrowserRouter(
  createRoutesFromElements(
    <Route path="/" element={<Layout />}>
      <Route index loader={homeLoader} element={<Home />} />
      <React.Fragment>
        <Route
          path="todos"
          action={todosAction}
          loader={todosLoader}
          element={<TodosList />}
          errorElement={<TodosBoundary />}
        >
          <Route path=":id" loader={todoLoader} element={<Todo />} />
        </Route>
      </React.Fragment>
      <Route
        path="deferred"
        loader={deferredLoader}
        element={<DeferredPage />}
      />
    </Route>
  )
);

Expected Behavior

No errors are raised, and the router behaves the same as if the routes within the fragment were not nested within a fragment.

Actual Behavior

Raises the error:

Uncaught Error: Found a route id collision on id "0-0".  Route id's must be globally unique within Data Router usages
    at invariant (react-router-dom.js?v=17e401c6:197:11)
    at react-router-dom.js?v=17e401c6:409:5
    at Array.map (<anonymous>)
    at convertRoutesToDataRoutes (react-router-dom.js?v=17e401c6:405:17)
    at react-router-dom.js?v=17e401c6:419:36
    at Array.map (<anonymous>)
    at convertRoutesToDataRoutes (react-router-dom.js?v=17e401c6:405:17)
    at createRouter (react-router-dom.js?v=17e401c6:1009:20)
    at createBrowserRouter (react-router-dom.js?v=17e401c6:3634:10)
    at app.tsx:30:14

I believe the issue stems from the fact that the ids are generated based on the position of the routes within the DOM, but in the case of fragments, the React.Fragment element is omitted in the path, so (in the above example) both the Home and TodosList routes have the path 0-0.

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