Giter Club home page Giter Club logo

react-native-lazy-index's Introduction

react-native-lazy-index

npm version

react-native-lazy-index is a RAM bundle friendly, bundle-time generated index.js. Improve your app startup time by only loading features you'll use on demand.

For information on RAM bundles and inline requires, see React Native Performance.

If you use Haul, also take a look at their documentation.

Installation

npm install --save react-native-lazy-index

Usage

react-native-lazy-index uses babel-plugin-codegen, so you'll need to configure Babel to include it. The recommended way is to add it to your .babelrc:

{
  "plugins": ["codegen"]
}

In your package.json, add a section called "experiences" with the features that should be lazy loaded. In the example below, we have four features keyed on unique names:

{
  "name": "MyAwesomeApp",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "@MyAwesomeApp/SomeFeature": "*",
    "@MyAwesomeApp/AnotherFeature": "*",
    "@MyAwesomeApp/YetAnotherFeature": "*",
    "@MyAwesomeApp/FinalFeature": "*",
    "react": "16.9.0",
    "react-native": "0.61.4",
    "react-native-lazy-index": "^1.0.0"
  },
  "experiences": {
    "Some": "@MyAwesomeApp/SomeFeature",
    "Another": "@MyAwesomeApp/AnotherFeature",
    "YetAnother": "@MyAwesomeApp/YetAnotherFeature",
    "Final": "@MyAwesomeApp/FinalFeature"
  }
}

Import react-native-lazy-index in your index.js:

import "react-native-lazy-index";

On the native side, you can now load your experiences by invoking ReactExperienceLoader.load(). As an example, we will load two features, AnotherFeature and YetAnotherFeature:

// iOS
[bridge enqueueJSCall:@"ReactExperienceLoader"
               method:@"load"
                 args:@[@"Another", @"YetAnother"]
           completion:nil];
// Android
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
CatalystInstance catalystInstance = reactContext.getCatalystInstance();

WritableNativeArray features = new WritableNativeArray();
features.pushString("Another");
features.pushString("YetAnother");

catalystInstance.callFunction("ReactExperienceLoader", "load", features);

Why

With a naive index.js, all features will be loaded when your app starts and React Native is initialized for the first time.

import "@MyAwesomeApp/SomeFeature";
import "@MyAwesomeApp/AnotherFeature";
import "@MyAwesomeApp/YetAnotherFeature";
import "@MyAwesomeApp/FinalFeature";

By loading features on demand, we can improve app startup time.

With react-native-lazy-index, we no longer load all features up front. Instead, index.js registers a callable module, ReactExperienceLoader, allowing full control over when a feature is loaded. Features that are never used, should never be loaded.

const BatchedBridge = require("react-native/Libraries/BatchedBridge/BatchedBridge");
BatchedBridge.registerCallableModule("ReactExperienceLoader", {
  load: (...names) =>
    names.forEach(name => {
      switch (name) {
        case "SomeFeature":
          return require("@MyAwesomeApp/SomeFeature");
        case "AnotherFeature":
          return require("@MyAwesomeApp/AnotherFeature");
        case "YetAnotherFeature":
          return require("@MyAwesomeApp/YetAnotherFeature");
        case "FinalFeature":
          return require("@MyAwesomeApp/FinalFeature");
      }
    })
});

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

react-native-lazy-index's People

Contributors

asmundg avatar dependabot[bot] avatar microsoft-github-operations[bot] avatar microsoftopensource avatar tido64 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.