Handling error with React-Navigation

rezaabaskhanian

RezaAbaskhanian

Posted on April 23, 2023

Handling error with React-Navigation

When using react-navigation in your React Native app, you may encounter HTTP status codes like 404 when navigating to a specific screen or route. Fortunately, react-navigation provides an easy way to handle these errors.

You can use the onError prop provided by react-navigation to handle HTTP status codes and other errors. This prop allows you to specify a function to be called when an error occurs during navigation.

Here's an example of how to handle a 404 error:

import { NavigationContainer } from '@react-navigation/native';

function App() {
  const handleNavigationError = (error) => {
    if (error.statusCode === 404) {
      // handle 404 error here
      console.log('404 error occurred');
    }
  };

  return (
    <NavigationContainer onError={handleNavigationError}>
      {/* Your navigation stack goes here */}
    </NavigationContainer>
  );
}

Enter fullscreen mode Exit fullscreen mode
šŸ’– šŸ’Ŗ šŸ™… šŸš©
rezaabaskhanian
RezaAbaskhanian

Posted on April 23, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related