reactjs - React:如何在应用程序中的功能中重定向?
问题描述
我在应用程序(与路由器相同级别)中有一个注销功能,该功能被传递到各个页面,因此用户可以从任何地方注销。调用函数时如何添加重定向?
我尝试使用this.props.history.push("/");
但得到错误Cannot read property 'push' of undefined
解决方案
使用 ( withRouter
) 高阶组件
样本
import React from "react";
import { withRouter } from "react-router-dom";
class App extends React.Component {
...
handleSubmit=()=>{
this.props.history.push("/path");
}
...
}
export default withRouter(App);
推荐阅读
- java - 有没有办法使用java在android中将docx转换为pdf?
- python - 如何从包含另一个列表中的关键字的嵌套列表中找到所有列表
- react-native - 反应原生选择器数据集更改时如何修复崩溃
- ruby-on-rails - 为什么`a.is_a?数组 && !a.empty?` 引发 NoMethodError?
- unicode - 使用 ICU 消息语法时,复数 `offset` 参数是否应该被代码替换?
- ios - 更新到 Xcode 11 后,Swift WKWebView “获取断言时出错”
- c++ - 虚幻引擎指针问题(不理解)
- javascript - 正确使用 useState 变量
- java - 无法对非静态字段进行静态引用
- azure - 从现有 VPN 创建 Azure 容器