how to use data that are sent by this.props.history?

Viewed 59

When I want to go from a route to another with this.props.history.push("/"), also I want to send some additional data, for example: this.props.history.push('/postDetail', {data: item}). But I do not know how where to define this data in the class where we went.

Thank you in advance

1 Answers

You can send a prop called state that comes from the router:

this.props.history.push({
 pathname: '/postDetail',
 state: { data: item }
});

So now your component attached with that route will have the location prop available, inside the location object lies the state that you have passed. More info here: https://reacttraining.com/react-router/web/api/history

Related