react-native-screens

repository·main·Indexed 25 days ago

https://github.com/software-mansion/react-native-screens

Native navigation primitives for React Native that expose native navigation container components to provide optimized screen rendering for iOS, Android, and other platforms. It is primarily intended as a dependency for navigation libraries like react-navigation. The library includes components such as ScreenContainer, Screen, ScreenStack, and ScreenStackItem to manage native view attachment and platform-native stack navigation.

Tokens
11.7K
Snippets
26
Records
61
Agent score
85%

What's inside react-native-screens

  1. Enable experimental react-freeze support

    main

    To use experimental support for react-freeze (which uses React Suspense to prevent parts of the component tree from rendering while keeping state untouched), call enableFreeze(true) in your entry file (e.g., App.js).

    Requirements:

    • React Native 0.68 or higher
    • react-navigation 5.x or 6.x
    • react-native-screens >= v3.9.0
    import { enableFreeze } from 'react-native-screens';
    
    enableFreeze(true);
  2. Configure Android MainActivity (Kotlin)

    main

    On Android, to prevent crashes during Activity restarts, override the onCreate method in your MainActivity.kt to set the fragmentFactory using RNScreensFragmentFactory.

    Note: The override must be placed directly in MainActivity, not inside MainActivityDelegate.

    import android.os.Bundle;
    import com.swmansion.rnscreens.fragment.restoration.RNScreensFragmentFactory;
    
    class MainActivity: ReactActivity() {
    
        //...code
    
        //react-native-screens override
        override fun onCreate(savedInstanceState: Bundle?) {
          supportFragmentManager.fragmentFactory = RNScreensFragmentFactory()
          super.onCreate(savedInstanceState);
        }
    }
  3. Configure Android MainActivity to prevent crashes

    main

    On Android, View state may not persist consistently across Activity restarts, which can cause crashes. To avoid this, you must override the onCreate method in your MainActivity (located at android/app/src/main/java/<your package name>/MainActivity.java or .kt) to discard persisted Activity state using RNScreensFragmentFactory.

    Note: The override must be placed directly in MainActivity, not inside MainActivityDelegate.

    import android.os.Bundle;
    import com.swmansion.rnscreens.fragment.restoration.RNScreensFragmentFactory;
    
    public class MainActivity extends ReactActivity {
    
        //...code
    
        //react-native-screens override
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            getSupportFragmentManager().setFragmentFactory(new RNScreensFragmentFactory());
            super.onCreate(savedInstanceState);
        }
    
        public static class MainActivityDelegate extends ReactActivityDelegate {
            //...code
        }
    }
  4. Install React Native Screens for Fabric

    main

    To use react-native-screens in a Fabric-enabled application, follow these steps based on your platform:

    General

    • Add the latest version of react-native-screens to your project.

    iOS

    1. Install required Ruby gems by running the following command in your application's root directory:
      rbenv exec bundle install
    2. Install pods using the command used for Fabric builds. You must run this command whenever a new native library is added:
      rbenv exec bundle exec pod install

    Android

    • No additional steps are required, provided your application is already configured to build with Fabric.
    rbenv exec bundle install
    rbenv exec bundle exec pod install
  5. Install react-native-screens

    main

    Install react-native-screens as a dependency to enable native navigation container components. It is designed to be used with full-featured navigation libraries like react-navigation rather than as a standalone library.

    For bare React Native projects:

    yarn add react-native-screens

    For Expo managed workflow:

    npx expo install react-native-screens
  6. Use `<ScreenContainer/>` to manage native view attachment

    main

    If you are building a navigation library, use <ScreenContainer/> to control which parts of the React component tree are attached to the native view hierarchy.

    <ScreenContainer/> acts as a container for one or more <Screen/> components. It monitors the activityState property of its children to decide which screens should be attached to the native view hierarchy. To maintain efficiency, you should aim to keep the number of active screens to a minimum (e.g., only the topmost view in a stack or the selected tab).

    <ScreenContainer>
      <Screen>{tab1}</Screen>
      <Screen activityState={2}>{tab2}</Screen>
      <Screen>{tab3}</Screen>
    </ScreenContainer>
  7. Run FabricExample on Android

    main

    To run the application on Android, ensure you have Java 11 active. You can verify your version using javac --version. If necessary, update your JAVA_HOME environment variable or adjust your Android Studio settings.

    Once Java 11 is configured, you can launch the app using yarn android or directly from Android Studio.

    yarn android