我们如何在 React-Router v4 中使用 this.props.history.push('/page')
传递参数?
.then(response => {
var r = this;
if (response.status >= 200 && response.status < 300) {
r.props.history.push('/template');
});
原文由 IshanGarg 发布,翻译遵循 CC BY-SA 4.0 许可协议
首先,你不需要做
var r = this;
因为 this inif statement
指的是回调本身的上下文,因为你使用箭头函数指的是 React 组件上下文。根据文档:
因此,在导航时,您可以将道具传递给历史对象,例如
或类似地用于
Link
组件或Redirect
组件然后在使用
/template
路由呈现的组件中,您可以访问传递的道具另请记住,当使用 props 中的历史记录或位置对象时,您需要将组件与
withRouter
连接。根据文档: