简体   繁体   English

如何使用 this.props.history 发送的数据?

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

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}) .当我想使用this.props.history.push("/")从一条路线转到另一条路线时,我还想发送一些额外的数据,例如: this.props.history.push('/postDetail', {数据:项目}) But I do not know how where to define this data in the class where we went.但是我不知道在我们去的类中如何定义这些数据。

Thank you in advance先感谢您

You can send a prop called state that comes from the router:您可以发送来自路由器的名为 state 的道具:

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更多信息在这里: https : //reacttraining.com/react-router/web/api/history

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM