Build for scale: Best approach?on How to use React Navigation in React Native
Malik Chohra
React Native Mobile Engineer | Sharing Insights on Software Engineering | Digital Nomad Lifestyle | Passionate about Team Collaboration, processes and modern technology
Introduction
This series of build for scale: Is About the advanced way to use React native app.
I will talk about methods, best practices, how-to use guides, and advanced approaches on how to use the most used tools, and packages. Taking into account how to structure it in your app, how to architect the solution, and advanced approaches for a performant, secure, and scalable app.
This series is perfect for React Native developers of all levels who want to take their skills to the next level and build apps that stand the test of time. Whether you’re a seasoned professional or just starting, you’ll find invaluable knowledge and practical guidance to transform your React Native development journey I explained in the previous article how to optimise
React Navigation is a popular library for managing navigation in React Native applications. It provides a flexible and customizable solution for navigating between screens and managing the navigation state.
In this article, I will walk you through how to use the navigation for React Native, how it works, and best practices when it comes to using it.
Installation?
For our example, we have React Native Vanilla, with Yarn as the Package manager.?
yarn add @react-navigation/native
yarn add @react-navigation/stack
yarn add react-native-screens react-native-safe-area-context
Link for Ios
npx pod-install ios
Link for Android
react-native-screens package requires one additional configuration step to properly work on Android devices. Edit MainActivity.kt or MainActivity.java file which is located under android/app/src/main/java/<your package name>/.
Add the highlighted code to the body of MainActivity class.
public class MainActivity extends ReactActivity {
// …
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
}
// …
}
and make sure to add the following import statement at the top of this file below your package statement:
import android.os.Bundle;
Check the Official documentation if you have ambiguities
How to Structure Your App Navigation
Let’s say we have the following:
an app that has a Tab with two screens: ProductList, and Setting.? When we click on a product in ProductList, it takes us to a Product Screen.
First rule: Put all your navigations into the same folder: navigation
create a folder in your src folder, named navigation and put all of your navigation handling inside
Second rule: Manage each navigation group inside the same?file
for our case, we will have the following:
Third Rule: Use Navigation Container props?
How to do it the right?way.
I will be sharing an example of code here, so you can see it in action.
领英推荐
Let’s start:
STEP 1:
For naming consistency, we add the prefix screen at the end for each screen naming: Product List → ProductListScreen?
STEP 2:
As we have two navigations. each one has its own navigation.
1- ProductNavigator.tsx
2- Tab Navigator, where we use the ProductNavigator, and add the SettingScreen: Take into account:
STEP 3:
Create your DeepLinking file:
STEP 4:
Put all the navigations inside your navigation/AppNavigation/index.tsx?
To handle the Deep link, by handling the user journey if he wants to navigate either at the ProductList screen or Product Screen.
STEP 5:
I have included the route type in AppScreens.
It can be used such as:
import AppScreens, {
AppScreenProps,
} from '~/navigation/AppNavigation/AppScreens';
type ProductScreenRouteType =
AppScreenProps<AppScreens.ProductScreen>['route'];
const route = useRoute<ProductScreenRouteType>();
const {productId}=route
Conclusion
In conclusion, React Navigation empowers you to build seamless navigation experiences within your React Native apps. By following the steps outlined above, you can effectively integrate navigation into your projects, allowing users to navigate between different screens and sections of your app with ease. Remember, React Navigation offers a wide range of features and customization options to cater to the specific needs of your app. Explore the official documentation and experiment to discover the full potential of React Navigation in your React Native development journey.
#reactnative #react #typescript #mobiledevelopment #engineering #patterns #bestpractices #memoryleak #performance #scaleApp #fastApp #userexperience