React native based SDK

1.1k views Asked by At

I am trying to create a react based SDK, (i.e) I will be having an Android/iOS package which will be the entry point for an App and will be using react code for using existing business logic. I am able to see React's AppRegistry.registerComponent as the entry point for react app. But for my use case I don't have any view dependency. I only have business logics. How should I create the bundle?

In Android and iOS, I will be writing a script to copy paste the bundle that is created using this RN package to the asset folder and from there I will use this index.bundle to create ReactInstanceManager

Please help me understand will this workout (React based SDK without view components). What are the things I need to do for making this SDK.

Note: This SDK can be integrated in existing App which already having react-native dependency for rendering some screens. My SDK should handle this use case as well.

1

There are 1 answers

1
Fco P. On

You may not like my answer, but keep in mind I have been working on a couple of complex react-native projects the last couple years. So I'm talking from experience.

1.- This is a terrible idea. 2.- Is also possible.

Why is a terrible idea? A) Because you will bundle the full size of react-native within an app, regardless of usage. B) Because react-native is a rendering engine (and a very costly one). Is not designed for headless use. It has a non-trivial startup time, so your SDK will not be available right after starting the app. By default, ReactActivity stops the JS engine as soon as something covers up the UI (Like a permission dialog), leading to interesting and funny problems if your SDK is meant to be called at the same time. The bridging model involves a lot of latency, by design. The react-native dependencies may not match the ones from the app, leading to another host of problems. Is far easier to read by attackers, since is a big script any IDE can reformat. And depending on what your SDK needs to do, you may need to write a more complex bunch of native code. Build steps will require the consumer to use yarn/npm to build the app if you have JS dependencies, and will force the consumer to integrate react-native's hacky gradle steps (and into their CI tools too). If a react-native library gives you problems, you will have to patch it. So, lots of chances for trouble.

That said, you can create a JS bundle from whatever file you want by using the bundling commands (react-native bundle). So you can pack a non-UI JS functions and load them directly in a ReactActivity. If the consumer app is meant to be oblivious of the process, you will have to make a Base activity & Base application, load Catalyst, pass your bundle to the reactInstance, load it up, and also create a JVM (kotlin/java) API exposing a singleton or something similar, which sends the commands using an Emitter. And this is just android. iOs will have a similar path, but with the added complexities of integrating your SDK via xCode.

I would recommend you, having gone through this, to save yourself the trouble and try with kotlin multiplatform instead. Is designed for this sort of scenario, android works out of the box, and integrating object C frameworks with xCode is far easier, and does not force the consumer to do anything strange. And will work far better, for free.