Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly reset navigation to another view

I am resetting the current view with this code

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

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

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.

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

Edit: if I check that shadowView is not null everything works fine

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

Is this a Bug in RN ?

like image 487
user567 Avatar asked Nov 08 '22 11:11

user567


1 Answers

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

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

like image 75
Tal Z Avatar answered Nov 15 '22 12:11

Tal Z