r/reactjs•
comstrader

How to pass id parameter to route path in react-router-dom v6?

Needs Help

I'm trying to pass an id in a path to a component. Here is my route-config.ts file:

...
import EditMovie from "./movies/EditMovie";

const routes = [
   ...
  {path: '/genres/edit/:id(\\d+)', component: 
EditGenre},
...
];

export default routes;

And the component file that isn't loading EditGenre.tsx:

import { useParams } from "react-router-dom"

export default function EditGenre(){
  const {id}: any = useParams();
  return (
    <>
      <h3>Edit Genre</h3>
      The id is {id}
    </>
  )
}

The other pages (routes) work fine, even "http://localhost:3000/genres/edit", except when I go to "http://localhost:3000/genres/edit/12" I get error:

No routes matched location "/genres/edit/12" And nothing loads below the nav bar.

I'm following the code from someone using react-router-dom v5 and am guessing it's changed in v6?

Posted Apr 30, 2022, 7:34 PM UTC

12
Loading comments…