如何正确重置导航到另一个视图

时间:2018-01-19 15:49:37

标签: react-native navigation-drawer react-navigation

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

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

有时它会起作用,有时我在Xcode中的Signal Sigabrt文件上出现RCTUIManager.m错误。我无法弄清楚问题何时发生。该函数发生错误

- (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];
  });
}

如果我删除了函数中的代码,那么每件事都可以。 当没有崩溃时,控制台始终打印警告

  

RCTView类型的视图#1430有一个阴影集,但无法计算   阴影有效。考虑设置背景颜色来解决这个问题,   或将阴影应用于更具体的组件。

但我没有任何使用阴影的视图,也无法弄清楚哪个反应原生元素正在做它。

编辑: 如果我检查shadowView不是null,一切正常

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

这是RN的Bug吗?

1 个答案:

答案 0 :(得分:1)

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

所以你可以派出这样的东西:

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

在自定义getStateForAction函数中处理此案例:

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)
}

您可以在此处详细了解React Navigation - Custom Navigation Actions