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

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


for react router v4 we can do things this way:

const history = useHistory();
 history.push({
      pathname: "/create-opinion/"+user,
      state:{  username:userName}
    };

and to get it from the other component simply:

const history = useHistory();

then to get username:

const username = history.location.state.username;

don't forget to import useHistory from react-router-dom

if you haven't install react-router-dom yet type:

$ npm install --save react-router-dom

Tags:

React Router