I am trying to pass multiple parameters in a url using React-Router 5.1 and the useParams hook in a Functional component.
However I encounter really strange behavior.
I paste the url into the client.
Url:
http://localhost:3000/onboarding/eventId=5e9a173f9166f800128c04d1&groupId=5e9a173f9166f800128c04dc&userId=5e9a173f9166f800128c04da
Path:
<Route path = '/onboarding/:eventId?/:groupId?/:userId?' exact component = {OnboardingViewController} />
Strange thing #1: I have to make the params optional, or the browser just hangs forever.
I fish them out using all these strategies:
var { eventId, groupId, userId } = useParams();
var { eventId } = useParams();
var { groupId } = useParams();
var { userId } = useParams();
Strange thing #2: Unfortunately when trying to use these params this happens:
{userId: undefined, eventId: "eventId=5e9a173f9166f800128c04d1&groupId=5e9a173f9166f800128c04dc&userId=5e9a173f9166f800128c04da", groupId: undefined}
The hook just takes the first param and interprets the rest a part of te first.
Strange thing #3: Since adding this url params query accessing the page laoding is extremely slow, multiple seconds.
What am I not seeing, doing wrong?
ANSWER:
What I was doing wrong: I was using url/eventId=123. This is wrong. You just need to supply the resource at the right place in the URL url/1/2/3.
correct:
http://localhost:3000/onboarding/5e9aaf4fc27583001190834e/5e9aaf60c275830011908361/5e9aaf4fc275830011908357
You then tell the Router that those things will be called eventId & groupId & userId.
<Route path = '/onboarding/:eventId/:groupId/:userId' exact component = {OnboardingViewController} />
Then you can access these in the component using the userPrams() hook.
var { eventId, groupId, userId } = useParams();
Thanks everyone!
Import { useParams } from 'react-router-dom'. As seen in the console. log, useParams returns an object of the Route parameters. This means we can deconstruct the id with { id } = useParams() to grab the id of the path and use the id param to dynamically fetch data from the api.
To get the url parameter from a current route, we can use the useParams() hook in react router v5. Consider, we have a route like this in our react app. Now, we can access the :id param value from a Users component using the useParams() hook. In React router v4, you can access it using the props.match.params.id .
The useParams hook returns an object of key/value pairs of the dynamic params from the current URL that were matched by the <Route path> . Child routes inherit all params from their parent routes.
Your Route structure and Route doesn't match
If you want to use params in your URL would be
http://localhost:3000/onboarding/5e9a173f9166f800128c04d1/5e9a173f9166f800128c04dc/5e9a173f9166f800128c04da
And your Route component would be:
<Route path = '/onboarding/:eventId/:groupId/:userId' exact component = {OnboardingViewController} />
And then you can use this in the OnboardingViewController
component:
var { eventId, groupId, userId } = useParams();
console.log(eventId,groupId,userId)
You are mixing up match parameters with URL query parameters.
The URL query parameters can be retrieved from the location
object using the useLocation
hook.
Given URL http://localhost:3000/onboarding/?eventId=5e9a173f9166f800128c04d1&groupId=5e9a173f9166f800128c04dc&userId=5e9a173f9166f800128c04da
{
pathname: '/onboarding/',
search: '?eventId=5e9a173f9166f800128c04d1&groupId=5e9a173f9166f800128c04dc&userId=5e9a173f9166f800128c04da'
}
would need a route path="/onboarding/"
though
You can use a QueryParameter processing library to then convert these to a map object.
If you could massage your URL to be in the form: http://localhost:3000/onboarding/5e9a173f9166f800128c04d1/5e9a173f9166f800128c04dc/5e9a173f9166f800128c04da
Then the route path='/onboarding/:eventId/:groupId/:userId'
can then match the path params returned by useParams
.
const { eventId, groupId, userId } = useParams();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With