如何正确将导航重置为其他视图 [英] How to properly reset navigation to another view

查看:188
本文介绍了如何正确将导航重置为其他视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码重置当前视图

I am resetting the current view with this code

NavigationActions.reset({ index: 0, key: null, actions: [ NavigationActions.navigate({ routeName: 'Login' }) ] });

有时它可以工作,有时我在Xcode中的RCTUIManager.m文件上收到一个Signal Sigabrt错误.我不知道问题何时发生.该函数中发生错误

Some times it works and some time I get a Signal Sigabrt error on the RCTUIManager.m file in Xcode. I can't figure out when did the problem occur. The error happens in this function

- (void)setSize:(CGSize)size forView:(UIView *)view
{
  RCTAssertMainQueue();

  NSNumber *reactTag = view.reactTag;
  dispatch_async(RCTGetUIManagerQueue(), ^{
    RCTShadowView *shadowView = self->_shadowViewRegistry[reactTag];
    RCTAssert(shadowView != nil, @"Could not locate shadow view with tag #%@", reactTag); // ERROR in this line

    if (CGSizeEqualToSize(size, shadowView.size)) {
      return;
    }

    shadowView.size = size;
    [self setNeedsLayout];
  });
}

如果我删除该函数上的代码,则一切正常. 没有崩溃时,控制台始终会打印警告

If I delete the code on the function every thing works fine. When there is no crashes, the console is always printing a warning

RCTView类型的View#1430设置了阴影,但无法计算 阴影有效.考虑设置背景色以解决此问题, 或将阴影应用于更具体的组件.

View #1430 of type RCTView has a shadow set but cannot calculate shadow efficiently. Consider setting a background color to fix this, or apply the shadow to a more specific component.

但是我没有任何使用shadow的View,也无法弄清楚哪个react-native元素正在执行此操作.

But I do not have any View using shadow and could not figure out which react-native element is doing it.

如果我检查shadowView不为null,则一切正常

if I check that shadowView is not null everything works fine

if(shadowView){
    RCTAssert(shadowView != nil, @"Could not locate view with tag #%@", reactTag);
    } 

这是RN中的错误吗?

推荐答案

我找到的解决方案是重写路由器的getStateForAction函数并处理重置.

The solution I found is to override the router's getStateForAction function and handle the reset.

因此,您可以调度以下内容:

So you could dispatch something like this:

NavigationActions.reset({ index: 0, key: null, actions: { navigateTo: 'Login' }});

并使用自定义的getStateForAction函数处理这种情况:

And handle this case in a custom getStateForAction function:

const defaultGetStateForAction = YourNavigator.router.getStateForAction

YourNavigator.router.getStateForAction = (action, state) => {
    if (action.type === NavigationActions.RESET && action.actions.navigateTo) {

      const updatedRoutes = [{routeName: action.actions.navigateTo}]

      return {
        ...state,
        routes: updatedRoutes,
        index: 0
      }
    }

    return defaultGetStateForAction(action, state)
}

在这里您可以了解有关反应导航-自定义导航操作的更多信息

Here you can read more about React Navigation - Custom Navigation Actions

这篇关于如何正确将导航重置为其他视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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