简体   繁体   English

是navigation.goBack(); navigation.navigate(...); 有效?

[英]Is navigation.goBack(); navigation.navigate(…); valid?

What's the recommended way to eliminate a screen from the stack? 从堆栈中消除屏幕的推荐方法是什么? I have a few cases where a user submits info on one screen (creating an account, or conducting a transaction, etc) and I'd like the input screen to be removed such that they're routed to the result screen and going back takes them to the screen prior to entering the info. 在某些情况下,用户在一个屏幕上提交信息(创建帐户或进行交易等),并且我希望删除输入屏幕,以便将他们路由到结果屏幕并返回在输入信息之前将其显示到屏幕上。

The ideal flow would be something like Item Screen -> Purchase Screen -> Result Screen --(goBack)--> Item Screen, to prevent confusion or double submission of collected info. 理想的流程应类似于物料屏幕->购买屏幕->结果屏幕-(goBack)->物料屏幕,以防止混乱或重复提交收集的信息。

What I'm doing currently is navigation.goBack(); navigation.navigate('ResultScreen'); 我目前正在做的是navigation.goBack(); navigation.navigate('ResultScreen'); navigation.goBack(); navigation.navigate('ResultScreen'); , but I'm getting warnings about setting state on an unmounted component (the message suggests this is a memory leak). ,但我收到有关在未安装的组件上设置状态的警告(消息表明这是内存泄漏)。 I don't see any obvious setState calls in my code on that path, so I'm thinking that either navigation.navigate() is doing a setState/forceUpdate (forceRender? whatever..) under the covers or that I'm otherwise not doing this in the intended way. 我在该路径上的代码中没有看到任何明显的setState调用,因此我在想Navigation.navigate()正在做一个setState / forceUpdate(forceRender吗?)。不按预期方式进行操作。

Searching the docs/stackoverflow/github issues hasn't been fruitful for guidance on this. 搜索docs / stackoverflow / github问题在此方面的指导并不成功。 Any suggestions? 有什么建议么?

Thanks! 谢谢!

The best way to achieve this so you don't over pollute your stack is by using the Reset action . 最好的方法是使用Reset动作 ,以确保您不会过度污染堆栈。

import { StackActions, NavigationActions } from 'react-navigation';

const resetAction = StackActions.reset({
  index: 0,
  actions: [
    NavigationActions.navigate({ routeName: 'ItemScreen' })
  ],
});
this.props.navigation.dispatch(resetAction);

This way you are resetting the stack and eliminating the possibility of someone re-navigating back through your various items. 这样,您就可以重置堆栈,并消除了有人重新浏览各个项目的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM