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

use*_*567 5 navigation-drawer react-native react-navigation

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

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

有时候它有效,有时我在Xcode中Signal SigabrtRCTUIManager.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];
  });
}
Run Code Online (Sandbox Code Playgroud)

如果我删除函数上的代码,每件事都可以正常工作.当没有崩溃时,控制台始终打印警告

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

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

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

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

这是RN中的Bug吗?

Tal*_*l Z 1

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

所以你可以发送这样的东西:

NavigationActions.reset({ index: 0, key: null, actions: { navigateTo: 'Login' }});
Run Code Online (Sandbox Code Playgroud)

并在自定义函数中处理这种情况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)
}
Run Code Online (Sandbox Code Playgroud)

在这里您可以阅读有关React Navigation - 自定义导航操作的更多信息