react-native-exception-handler
react-native-exception-handler copied to clipboard
How to get the screen name on which the error occurs
HI, How can we get the screen name on which the error occurs? It is only returning the error message.
Did you find anything on this issue?, i am looking for the same.
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!
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 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.
I would also like to know how to get the name of the screen on which the error occured.