score:31

Accepted answer

you can only use useparams in a component that is a child of your router component, but app is the parent in your case.

the router component injects the context containing the match into the tree below it which will be read by the useparams hook by internally using the usecontext hook.

score:3

you can use this code , in jest test file for useparams .this worked for me

 jest.mock('react-router-dom', () => ({
  ...jest.requireactual('react-router-dom'),
  useparams: jest.fn().mockreturnvalue({ environment: 'dev', service: 'fakeservice' }),
}))

hope this works !

score:4

for anyone else seeing this error from trying to use useparams to get query/search parameters and not url parameters, you can use uselocation from react-router-dom

import { uselocation } from 'react-router-dom';
...
...
const query = new urlsearchparams(uselocation().search);
...

difference between query parameters and url parameters

query parameter demo

brwoser support for urlsearchparams()


Related Query

More Query from same tag