react-native-exception-handler icon indicating copy to clipboard operation
react-native-exception-handler copied to clipboard

How to get the screen name on which the error occurs

Open faizanbuggy opened this issue 2 years ago • 5 comments

HI, How can we get the screen name on which the error occurs? It is only returning the error message.

faizanbuggy avatar Oct 01 '21 12:10 faizanbuggy

Did you find anything on this issue?, i am looking for the same.

prpgan avatar Dec 06 '21 08:12 prpgan

Not by this library. But got a workflow for this by using react-navigation. So, whenever the error occurs I am getting the screen from the react-navigation. This screen would be prior to the error screen. Hope this would help you!

faizanbuggy avatar Dec 06 '21 09:12 faizanbuggy

In case you use react navigation (v6 but might work also in v5) you can use global variable. First you need to find a way to get the current active screen and when the active screen changes set the global variable to the current screen

//function

const onStateChange = async () => {
    const previousRouteName = routeNameRef.current;
    const currentRouteName = navigationRef?.getCurrentRoute && navigationRef?.getCurrentRoute()?.name;

    if (currentRouteName) {
       (global as any).currentRouteName = currentRouteName; //------set the current route name to global variable
      routeNameRef.current = currentRouteName;
    }
  };
  const onReady = () => {
    const name = (navigationRef?.getCurrentRoute && navigationRef?.getCurrentRoute()?.name) || '';
    if (name) {
      (global as any).currentRouteName = name;
      routeNameRef.current = name;
    }
  };

//in your navigation container

 <NavigationContainer
      ref={navigationRef}
      onReady={onReady}
      onStateChange={onStateChange}
      theme={isDark ? DarkTheme : DefaultTheme}
    >...</NavigationContainer>

Then you can access your current route name from global variable (global as any).currentRouteName

ssokhavirith avatar Jan 10 '22 03:01 ssokhavirith

@ssokhavirith but this return the second last screen not the current screen on which error has occured. For instance; I have a stack screen inside a bottom navigation and error occurs on the stack screen. So the currentscreen always points to the bottom tab in which the stack screen (error occured screen) lies.

faizanbutt avatar Jan 10 '22 14:01 faizanbutt

I would also like to know how to get the name of the screen on which the error occured.

mednche avatar Feb 18 '23 11:02 mednche