- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个后退按钮,可以让用户返回一个屏幕,但是当没有屏幕可以返回时,我希望它做些别的事情,所以这是我的代码:
<Button onPress={()=>{
if(CanGoBack){ // imaginary 'CanGoBack' variable
this.props.navigation.goBack()
}else{
this.doSomething()
}
}}/>
我怎样才能做到这一点?
最佳答案
Note this answer was originally written for
react-navigation
v3.3.0
. You should check the current documentation forreact-navigation
to make sure that this is still supported and if there are any changes/easier methods.
this.props.navigation
中有一个名为dangerouslyGetParent
的函数。
它在 documentation 中陈述了以下内容:
Another good use case for this is to find the index of the activeroute in the parent's route list. So in the case of a stack if you areat index 0 then you may not want to render a back button, but ifyou're somewhere else in the list then you would render a back button.
所以我们可以使用下面的方法来获取路由的索引
this.props.navigation.dangerouslyGetParent().state.index
所以我们可以在您的 Button
的 onPress
中使用它以下列方式检查我们是否回到了路线的起点。
<Button onPress={() => {
// get the index of the route
let index = this.props.navigation.dangerouslyGetParent().state.index;
// handle the index we get
if (index > 0) {
this.props.navigation.goBack();
} else {
this.doSomething();
}
}}/>
来自documentation ,我们可以看到 dangerouslyGetParent
仍然存在。
This method returns the navigation prop from the parent navigator thatthe current navigator is nested in. For example, if you have a stacknavigator and a tab navigator nested inside the stack, then you canuse dangerouslyGetParent inside a screen of the tab navigator to getthe navigation prop passed from the stack navigator.
所以可以在v5中对v3使用上述方法。
还有一个未记录的 api,称为 canGoBack()
。这可以通过以下方式从导航 Prop 访问:
this.props.navigation.canGoBack()
如果您能够返回堆栈,此属性将返回一个 bool 值。这意味着我们可以通过以下方式更新我们为 v3
所做的代码。
<Button onPress={() => {
// check to see if it is possible to go back
let canGoBack = this.props.navigation.canGoBack();
// handle what we do it we can/cannot go back
if (canGoBack) {
this.props.navigation.goBack();
} else {
this.doSomething();
}
}}/>
但是,由于这是一个未记录的 API,它很可能会更改,因此依赖它可能会有风险。
现在看来 canGoBack
已经记录在案了,您可以找到信息 here
This method returns a boolean indicating whether there's any navigation history available in the current navigator, or in any parent navigators. You can use this to check if you can call
navigation.goBack()
:
if (navigation.canGoBack()) {
navigation.goBack();
}
Don't use this method for rendering content as this will not trigger a re-render. This is only intended for use inside callbacks, event listeners etc.
关于javascript - 如何检查 goBack() 函数在 react 导航中是否可行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54700512/
在我的 React Native 应用程序中,我有三个屏幕,如 A 和 B。我使用 react-navigation 版本 5 从一个屏幕导航到另一个屏幕。 当我从 A 移动到 B 并在 B 中输入一
场景是我有一个日历页面,该页面显示一个月中的所有日子,每天都有一个添加按钮: 当我单击添加按钮时,它会将我带到另一个页面,如果用户点击硬件后退按钮,我希望应用程序返回日历页面,但我希望页面滚动到用户最
我正在创建一个应用程序,我登录/注销有一个主页、关于、个人资料等。它也使用 react 路由器,但是当我登录时,我希望应用程序转换到它所在的最后一个路由,它有时会起作用,例如,如果我访问 about,
我如何错误检查Chrome浏览器是否有前一页?如果 Chrome 实例刚刚启动,并且没有上一页要使用命令page进入,我有一个冻结的按钮。 最佳答案 这是一个错误,不是预期的行为。您应该考虑将其报告为
我是 react 原生新手。现在我需要在两个屏幕上实现一个功能。当我单击“addActivity”场景中的底部时,它将返回“homeScene”,即“addActivity”的前一个场景,并显示“ad
我一直在阅读许多类似的问题和答案,但仍然不太明白这里出了什么问题。 我正在尝试创建一个按钮以返回上一个 View ,但我不断收到标题中提到的错误。有人可以告诉我我做错了什么吗? UIImage *i
我从一个不是tab的页面跳转到tab页。 在tab页,点击item后想返回。我使用 $ionicHistory.goBack() 返回。 我发现一个奇怪的情况,第一次$ionicHistory.goB
我正在尝试在按下 goBack 后重新渲染组件。通过使用 goBack 我可以返回页面,但该页面不会刷新。我该如何解决?goBack 之后我必须调用一些 api,现在在 back non of the
我的应用程序有一个要求,我需要在 webview 中呈现多个网页并提供 goback() 方法。 但不幸的是 goback() 方法在我们加载不退出的 url(来自多个页面的 url 之一)的情况下不
我正在开发一个使用 WKWebView 和自定义导航按钮的 iOS 应用程序。我们发现点击某些链接需要使用我们的后退按钮两次,因为第一次调用似乎只是重新加载当前页面。这不会发生在移动版 Safari
我正在使用reactjs构建一个应用程序,在某些时候我有一个SearchForm,然后我单击其中一个项目,在该 View 中我有一个后退按钮。 我想要的是用以前的数据填充搜索表单。我已经通过 java
我在页面中有产品列表。这个产品列表只会显示 5 个。单击“显示额外”按钮后,它将显示剩余的产品。 好的,在我点击“显示额外”按钮后,它会显示接下来的 5 个产品。现在共有 8 个产品。在剩下的 5 个
这是主要 Activity 的xml代码..
我创建了一个简单的应用程序来重现该问题。我有一个带有相关链接的 html 文件(带有 # 符号)。我正在将文件加载到 Android 的 WebView 中。但我只能通过链接访问一次。返回后,当我点击
我已经设置了一个 StackNavigator,它会触发一个 redux 操作来获取 componentDidMount 上的数据,在导航到另一个屏幕并返回到上一个屏幕后,componentDidMo
我有两个屏幕。每个屏幕包含三个子类。是的,我正在使用每个屏幕的选项卡。在我的第一个屏幕上说。 主屏幕 有 -> ScreenA、ScreenB、ScreenC。我的 DetailScreen 有 ->
我想在从另一个页面导航回来时向下滚动到 GridView 的底部。更具体地说,Frame.GoBack(NavigationTransitionInfo) 结束时,滚动将可见。是否有回调或某种方式在转
我面临返回上一页的问题,如果有人帮助我,将非常感谢。 这是我的 Controller 登录操作 public function actionLogin() { $this->l
关于TWebBrowser方法,我在GoBack上遇到问题。以下是显示问题的导航历史记录: [1] htt#://www.google.com/ [2] htt#://news.google.com/
我创建了一些动画,我想用它们在我的应用程序中的路线之间导航。我在某些页面上有一个可见的后退按钮,允许用户弹出导航堆栈以访问最近的页面。我想要两种不同的动画,一种用于在堆栈中更深入地导航,另一种用于弹回
我是一名优秀的程序员,十分优秀!