reactjs - 如何禁用 React Router 中的后退按钮?
问题描述
我试图禁用浏览器中的后退按钮,但它只是将页面重新加载到 path="/"
如何禁用浏览器的后退按钮,所以当用户单击它时没有任何反应?
export default class Main extends Component {
componentDidMount() {
const history = createBrowserHistory();
history.listen((newLocation, action) => {
if (action === "POP") {
history.go(0);
}
});
}
render() {
return (
<div>
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/main/component1" component={Component1} />
<Route path="/main/component2" component={Component2} />
<Route path="/main/component3" component={Component3} />
</Switch>
</Router>
</div>
);
}
}
解决方案
您可以使用 react-router-dom 来完成
import { useHistory } from "react-router-dom";
let history = useHistory();
history.replace("/login");
推荐阅读
- java - Vaadin 中按钮的多行字幕不起作用
- javascript - Firebase 功能运行速度极慢
- c++ - 使用静态成员时有关 C++ 中内存分配的问题
- kotlin - Kotlin 扫描器 - 验证 nextLine() 用户输入
- c++ - 如何检查 char 编码是否为 ASCII?
- c# - Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension 类型中的方法“get_Info”没有实现
- node.js - 从 Rest API 中的 repo 获取数据
- sql - 在 Case 表达式中使用子查询
- html - 为什么我不能在 div 中设置离子按钮的样式
- android - 在 null 上调用了方法“文本”