I recently ran into a situation where I was using React Router and needed to pass a parameter within history to a component. Here’s how I went about it:
history.push({
pathname: '/newpage',
state: {
nameOfProperty: value
},
});
We create a key/value pair in the state
object above and create a name for the key that we want to associate the value with. In the receiving component, we then extract that value by doing the following:
const variableName = history.location.state?.nameOfProperty;
We add a question mark in order to prevent the app from throwing an error if there is no value in the property.