React-router get optional query parameter

i have been looking into some examples on stack overflow for defining optional query params in react-router and finally found out that it can be done like

 {
    path: '/forms/simulacao/:journeyId?',
    exact: true,
    name: 'Home',
    component: Home,
    authRequired: true
  }

so at next step i want to retrive the query param that is “journyeId” if it is there, so i tried with

const history=useHistory()
console.log(history.location)  

but that doesn’t seems to work, what should be the workaround for this ??

“react-router”: “^5.1.2”, “react-router-dom”: “^5.1.2”,

Answer

there is a hook for this: useParams from react-router-dom

<Route path="/item/:id" component={NewsPage} />

After that, in NewsPage component I will be able to grab an ID from route like this:

const params = useParams()
fetchNews(params.id).then(data => setNews(data))

And so on