How do you read a URL parameter in React Router?

React routing and forms · Freshers

Model answer

Define the route with a parameter, path="/users/:id", and read it in the component with const { id } = useParams(). The value is always a string, so convert it if you need a number.

For query strings like ?page=2, use useSearchParams.

Learn this topic

React Router: Multiple Pages, Links, Params, Layouts and Protected Routes in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions