Search code examples
reactjsurlreact-routerreact-router-dompath-parameter

How is path parameter name conflicts handled in react-router-dom?


home/user/:id/friends/:id
function SomeComponent() {
  const { id } = useParams();
}

Which id will be used here? There are name conflicts.


Solution

  • The last parameter of the path is the value that any component will see.

    This is trivial to test.

    Example:

    const Component = () => {
      const { id } = useParams();
    
      return <h1>Id: {id}</h1>;
    };
    
    export default function App() {
      return (
        <div className="App">
          <Link to="/home/user/123/friends/456">Test?</Link>
    
          <Routes>
            <Route path="home/user/:id/friends/:id" element={<Component />} />
          </Routes>
        </div>
      );
    }
    

    Edit how-is-path-parameter-name-conflicts-handled-in-react-router-dom

    enter image description here

    Don't use the same path parameter twice in the same path string per route.