Kabir Nigam Kabir Nigam - 2 months ago 14
React JSX Question

How to pass object to hashHistory.push() in react

Anyone know how to pass an object to hashHistory.push()?

Here's a code snippet:

redirectToDrug(id, drug) {
hashHistory.push(`/infusions/library/${id}`, drug);
}


Trying to get the drug information in there so that I dont have to query the database again for the drug info, as I already have it in the parent class.

Answer

You can pass object with query to push function:

router.push({
  pathname: `/infusions/library/${id}`,
  query: { drug }
})

And then to access your object in /infusuons/library/${id} page:

this.props.location.query.drug

Link to official docs

It is good solution if your object isn't too complex, but if drug is a big structure of data, I'd use unidirectional data flow framework, for example Redux.