如何知道反应路由器是否可以返回以在反应应用程序中显示后退按钮 [英] How to know if react-router can go back to display back button in react app

查看:43
本文介绍了如何知道反应路由器是否可以返回以在反应应用程序中显示后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎没有 API 方法可以检测您是否可以从应用程序的当前页面返回.可以检查历史长度,但这包括向前和向后堆栈.

There seems to be no API way to detect if you can go back from the current page in an app. One can check the history length, but that includes the forward and backward stack.

我只想在用户真正可以返回时才显示返回按钮.

I want to display the back button only when the user actually can go back.

推荐答案

你可以通过检查 history.action 如果它的值是 POP ,那么就没有回去的路线.这是我找到的唯一方法.

You can do it by checking history.action if its value is POP, then there is no route to go back. That's the only way I found.

<Button
  onClick={() => {
    if (history.action !== 'POP') history.goBack();
  }}
  disabled={history.action === 'POP'}
/>

这篇关于如何知道反应路由器是否可以返回以在反应应用程序中显示后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆