Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In React Router, how do you pass route parameters when using browser history push?

Tags:

react-router

Using the newer version of React Router, how do you pass parameters to the route you are transitioning to if you are transitioning using the browserHistory.push()? I am open to using some other way to transitioning if browserHistory does not allow this. I see that in my top most child component in React Router there is this.props.routeParams but I can't seem to get this populated with values I want from the previous route when transitioning.

like image 422
jiminssy Avatar asked Jul 30 '16 21:07

jiminssy


People also ask

How do you pass parameters with history push?

To pass parameters with history. push , Link , or Redirect in React Router v4, we can set the state property. to call history. push with an object with the state property to pass props to the history object.

How do you pass parameters in react route?

To pass parameters to component with React Router, we can use the useParams hook. <Route path="/details/:id" component={DetailsPage} />; to add the id parameter to our route. import { useParams } from 'react-router'; export default function DetailsPage() { const { id } = useParams(); // ... }


1 Answers

Now you will have do something like this

history.push({   pathname: '/about',   search: '?the=search',   state: { some: 'state' } }) 

Here is the link for API documentation

like image 165
Deadfish Avatar answered Sep 24 '22 13:09

Deadfish