### Run Native Navigation Example App on iOS Source: https://github.com/airbnb/native-navigation/blob/master/CONTRIBUTING.md These commands start the development packager and then launch the example application on an iOS simulator or device. ```bash npm start npm run run:ios ``` -------------------------------- ### Set up Native Navigation Development Environment Source: https://github.com/airbnb/native-navigation/blob/master/CONTRIBUTING.md This snippet provides the initial steps to clone the repository, navigate into it, and install project dependencies for the `native-navigation` project. ```bash git clone https://github.com/airbnb/native-navigation.git cd native-navigation npm install ``` -------------------------------- ### Run Native Navigation Example App on Android Source: https://github.com/airbnb/native-navigation/blob/master/CONTRIBUTING.md These commands start the development packager and then launch the example application on an Android emulator or device. ```bash npm start npm run run:android ``` -------------------------------- ### Install Native Navigation Project Dependencies Source: https://github.com/airbnb/native-navigation/blob/master/README.md Commands to install npm and bundler dependencies required to run the native-navigation example project. ```bash npm install ``` -------------------------------- ### Build and Watch Native Navigation Documentation Locally Source: https://github.com/airbnb/native-navigation/blob/master/CONTRIBUTING.md This command starts a local server to build and serve the project documentation, automatically refreshing the browser on source file changes. ```bash npm run docs:watch ``` -------------------------------- ### Initialize ReactNavigationCoordinator in MainApplication.java onCreate() Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Modifies the `onCreate()` method in `MainApplication.java` to initialize `SoLoader` and inject the `ReactInstanceManager` into `ReactNavigationCoordinator`, starting the navigation coordinator. ```Java import com.airbnb.android.react.navigation.ReactNavigationCoordinator; // ... @Override public void onCreate() { super.onCreate(); SoLoader.init(this, /* native exopackage */ false); // These three lines need to be added ReactNavigationCoordinator coordinator = ReactNavigationCoordinator.sharedInstance; coordinator.injectReactInstanceManager(mReactNativeHost.getReactInstanceManager()); coordinator.start(this); } ``` -------------------------------- ### Start Native Navigation Development Server Source: https://github.com/airbnb/native-navigation/blob/master/README.md Command to start the development server for the native-navigation project. ```bash npm start ``` -------------------------------- ### Complete AppDelegate Implementation for Native Navigation Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md This comprehensive `AppDelegate.m` file consolidates all the steps for integrating Native Navigation, including `RCTBridge` initialization, `ReactNavigationCoordinator` setup, root view controller configuration, and delegate method implementations for `RCTBridge` and `ReactNavigationCoordinator`. ```Objective-C #import "AppDelegate.h" #import @import NativeNavigation; @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; ReactNavigationCoordinator *coordinator = [ReactNavigationCoordinator sharedInstance]; [coordinator setBridge:bridge]; [coordinator setDelegate:self]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ReactViewController *mainViewController = [[ReactViewController alloc] initWithModuleName:@"Home"]; self.window.rootViewController = [[coordinator navigation] makeNavigationControllerWithRootViewController:mainViewController]; [self.window makeKeyAndVisible]; return YES; } - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil]; } - (UIViewController *)rootViewControllerForCoordinator: (ReactNavigationCoordinator *)coordinator { return self.window.rootViewController; } @end ``` -------------------------------- ### Run Native Navigation Example on Android Source: https://github.com/airbnb/native-navigation/blob/master/README.md Command to run the native-navigation example project on an Android emulator or device. ```bash npm run run:android ``` -------------------------------- ### Run Native Navigation Example on iOS Source: https://github.com/airbnb/native-navigation/blob/master/README.md Command to run the native-navigation example project on an iOS simulator or device. ```bash npm run run:ios ``` -------------------------------- ### Create New React Native Project Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md These commands initialize a new React Native project named 'MyNewProject' and then navigate into its directory. This is the first step after installing the CLI and before adding Native Navigation. ```bash react-native init MyNewProject cd MyNewProject ``` -------------------------------- ### Install Pod Dependencies Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Runs CocoaPods to install the dependencies defined in the Podfile. This command creates an Xcode workspace file (`.xcworkspace`) that should be used for opening the project going forward. ```bash pod install ``` -------------------------------- ### Install React Native CLI Globally Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md This command installs the React Native command-line interface globally on your system. It is a prerequisite for creating and managing React Native projects. ```bash npm i -g react-native-cli ``` -------------------------------- ### Initialize Podfile for iOS Project Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Navigates into the iOS project directory and initializes a new Podfile. This file will be used to define and manage project dependencies via CocoaPods. ```bash cd ios pod init ``` -------------------------------- ### Example Usage of `registerConnectedScreen` Source: https://github.com/airbnb/native-navigation/blob/master/docs/guides/project-structure.md Illustrates how to use the custom `registerConnectedScreen` utility in `index.js` to register a screen. This example shows passing a `getStore` option to automatically connect the `SomeScreen` component to the Redux store. ```javascript // index.js import registerConnectedScreen from './utils/registerConnectedScreen'; registerConnectedScreen( 'SomeScreen', () => require('./screens/SomeScreen'), { getStore: () => require('./path/to/redux/store'), }, ); ``` -------------------------------- ### Check Native Navigation Codebase for Linting Compliance Source: https://github.com/airbnb/native-navigation/blob/master/CONTRIBUTING.md This command runs ESLint to check the source code against the Airbnb Styleguide, ensuring code quality and consistency. ```bash npm run lint ``` -------------------------------- ### Extend NativeNavigation ReactActivity and define initial screen in MainActivity.java Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Updates `MainActivity.java` to extend `com.airbnb.android.react.navigation.ReactActivity` and replaces `getMainComponentName()` with `getInitialScreenName()`, specifying the initial screen name for Native Navigation. ```Java import com.airbnb.android.react.navigation.ReactActivity; public class MainActivity extends ReactActivity { /** * Returns the name of the screen registered from JavaScript. */ @Override protected String getInitialScreenName() { return "Home"; } } ``` -------------------------------- ### Install CocoaPods Gem Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Installs the CocoaPods dependency manager using RubyGems. This is a prerequisite for managing iOS project dependencies with CocoaPods. ```bash sudo gem install cocoapods ``` -------------------------------- ### Install Native Navigation Dependency Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md This command installs the `native-navigation` package as a dependency in your React Native project. It saves the package to your `package.json` file. Note that `react-native link` is not supported for this package. ```bash npm i --save native-navigation ``` -------------------------------- ### Complete AppDelegate Header File for Native Navigation Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md This is the full `AppDelegate.h` file, demonstrating the necessary imports (`UIKit`, `React/RCTBridge.h`, `NativeNavigation`) and protocol adoptions (`UIApplicationDelegate`, `RCTBridgeDelegate`, `ReactNavigationCoordinatorDelegate`) required for Native Navigation integration. ```Objective-C #import #import @import NativeNavigation; @interface AppDelegate : UIResponder @property (nonatomic, strong) UIWindow *window; @end ``` -------------------------------- ### Add NativeNavigationPackage to MainApplication.java getPackages() Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Imports `NativeNavigationPackage` and adds it to the list of `ReactPackage`s returned by `getPackages()` in `MainApplication.java`, integrating Native Navigation with the React Native package system. ```Java import com.airbnb.android.react.navigation.NativeNavigationPackage; // ... @Override protected List getPackages() { return Arrays.asList( new MainReactPackage(), new NativeNavigationPackage() ); } ``` -------------------------------- ### Open Xcode Workspace Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Opens the newly created Xcode workspace file. It is crucial to use this `.xcworkspace` file instead of the original `.xcodeproj` file after CocoaPods installation to ensure all dependencies are correctly linked. ```bash open MyNewProject.xcworkspace ``` -------------------------------- ### Include Native Navigation in Android settings.gradle Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Configures `android/settings.gradle` to include Native Navigation as a source project, enabling the build system to compile it directly from `node_modules`. ```Gradle include ':native-navigation' project(':native-navigation').projectDir = new File(rootProject.projectDir, '../node_modules/native-navigation/lib/android') ``` -------------------------------- ### Configure packagingOptions in Android app build.gradle Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Adds a `packagingOptions` block to the `android` section of `android/app/build.gradle` to exclude various META-INF files, preventing potential conflicts during the build process. ```Gradle packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/DEPENDENCIES.txt' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/notice.txt' exclude 'META-INF/license.txt' exclude 'META-INF/dependencies.txt' exclude 'META-INF/LGPL2.1' } ``` -------------------------------- ### Add Native Navigation dependencies to Android app build.gradle Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Adds required Android support libraries and the Native Navigation project dependency to the `dependencies` block in `android/app/build.gradle`. ```Gradle dependencies { // ... any dependencies you already had // if you don't already depend on the support libraries, add these compile 'com.android.support:appcompat-v7:25.2.0' compile 'com.android.support:support-annotations:25.2.0' // add native-navigation compile project(':native-navigation') } ``` -------------------------------- ### Initialize React Native Bridge (RCTBridge) Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md This snippet demonstrates how to create an instance of `RCTBridge`, which is essential for React Native to communicate with native modules. The `AppDelegate` typically acts as its delegate. ```Objective-C RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; ``` -------------------------------- ### Define a React Native Screen Component Source: https://github.com/airbnb/native-navigation/blob/master/docs/api/navigator/registerScreen.md Example of a basic React component (`ScreenOne.js`) that can be registered as a native screen. This component serves as the definition for screens used in `registerScreen` examples. ```js // ScreenOne.js import React from 'react'; export default class ScreenOne extends React.Component { render() { // ... } } ``` -------------------------------- ### Configure Podfile for Native Navigation and React Native Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Edits the Podfile to include Native Navigation, React Native (with necessary subspecs like Core, RCTText, RCTNetwork, RCTWebSocket, RCTAnimation, RCTImage), and Yoga. It also enables `use_frameworks!` for Swift compatibility, which is required by Native Navigation. ```ruby target 'MyNewProject' do # Native Navigation uses Swift, so this line is required! use_frameworks! # Native Navigation! pod 'native-navigation', :path => '../node_modules/native-navigation' # To use CocoaPods with React Native, you need to add this specific Yoga spec as well pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga/Yoga.podspec' # You don't necessarily need all of these subspecs, but this would be a typical setup. pod 'React', :path => '../node_modules/react-native', :subspecs => [ 'Core', 'RCTText', 'RCTNetwork', 'RCTWebSocket', # needed for debugging 'RCTAnimation', 'RCTImage', 'RCTNetwork' # Add any other subspecs you want to use in your project ] # Add any other dependencies here, including any 3rd party native libraries that you depend on for # React Native. end ``` -------------------------------- ### Verify CocoaPods Version Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md Checks the installed version of CocoaPods. React Native requires version 1.2.0 or greater, so it's important to verify the current version. ```bash pod --version ``` -------------------------------- ### Register React Native Root Component (Standard) Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md This JavaScript snippet shows the standard way to register the main component of a React Native application using `AppRegistry.registerComponent`. It defines the entry point for the application before Native Navigation modifications. ```js AppRegistry.registerComponent('MyNewProject', () => MyNewProject); ``` -------------------------------- ### Configure Shared Native Navigation Coordinator Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md The `ReactNavigationCoordinator` is a central component of the Native Navigation library. This code shows how to obtain its shared instance and provide it with the initialized `RCTBridge` and its own delegate. ```Objective-C ReactNavigationCoordinator *coordinator = [ReactNavigationCoordinator sharedInstance]; [coordinator setBridge:bridge]; [coordinator setDelegate:self]; ``` -------------------------------- ### Set App's Root View Controller with Native Navigation Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md This code snippet illustrates how to create the application's initial React Native screen (`ReactViewController`) and wrap it within a navigation controller provided by Native Navigation, then assign it as the window's root view controller. ```Objective-C ReactViewController *mainViewController = [[ReactViewController alloc] initWithModuleName:@"Home"]; self.window.rootViewController = [[coordinator navigation] makeNavigationControllerWithRootViewController:mainViewController]; ``` -------------------------------- ### Example Usage of Navigator.SharedElement in React Native Source: https://github.com/airbnb/native-navigation/blob/master/docs/api/navigator-shared-element.md Demonstrates how to use the `` component to wrap an `Image` component, enabling shared element transitions. This example shows a `UserImage` component that uses `SharedElement` with `type` and `typeId` props to identify a user's profile image for transitions. ```jsx import React from 'react'; import { Image } from 'react-native'; import { SharedElement } from 'native-navigation'; export default class UserImage extends React.Component { render() { const { user } = this.props; return ( ); } } ``` -------------------------------- ### Provide Root View Controller for Native Navigation Coordinator Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md The `rootViewControllerForCoordinator:` delegate method of `ReactNavigationCoordinatorDelegate` is used by Native Navigation to understand the app's current navigation stack. It should return the main root view controller of the application window. ```Objective-C - (UIViewController *)rootViewControllerForCoordinator: (ReactNavigationCoordinator *)coordinator { return self.window.rootViewController; } ``` -------------------------------- ### Test React Native Welcome Screen Rendering Source: https://github.com/airbnb/native-navigation/blob/master/docs/installation.md This test method demonstrates how to programmatically check if a specific UI element (e.g., a welcome screen) is rendered in a React Native application's view hierarchy within a timeout period. It's useful for UI testing. ```Objective-C - (void)testRendersWelcomeScreen { UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS]; BOOL foundElement = NO; while ([date timeIntervalSinceNow] > 0 && !foundElement) { [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; [[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; foundElement = [self findSubviewInView:vc.view matching:^BOOL(UIView *view) { if ([view.accessibilityLabel isEqualToString:TEXT_TO_LOOK_FOR]) { return YES; } return NO; }]; } XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS); } ``` -------------------------------- ### Example Usage of `present` function in React Native Source: https://github.com/airbnb/native-navigation/blob/master/docs/api/navigator/present.md Demonstrates how to use the `native-navigation` `present` function within a React component to navigate to a 'Register' screen and handle the result upon dismissal. ```javascript import React from 'react'; import Navigator from 'native-navigation'; class Foo extends React.Component { constructor(props) { super(props); this.state = { registered: false, }; this.onPress = this.onPress.bind(this); } onPress() { return Navigator .present('Register', { source: 'Foo' }) .then(({ code }) => this.setState({ registered: code === Navigator.RESULT_OK })); } render() { return ( {!registered && (