I use RN 0.66.3 and want to make direct sync calls from javascript to native code in my iOS React Native project to share data without using the React Native Bridge for performance purposes so that I need to have a shared global object and access to its properties and methods from javascript.
I know that is possible with JSI (JavaScript Interface) but there are no docs and few tutorials about so what the simple steps or sample code to implement this?
To expose your object to javascript over React Native JSI you should make next steps:
HostObject
get
andset
methods to implement access to its properties and methods.Look at
NativeStorage
sample that can store key/value pairs persistently toNSUserDefaults
across launches of your app:NativeStorage class
AppDelegate.mm
App.js
Outputs:
Future React Native's TurboModules & CodeGen makes all of this cleaner & easier but it's the low level JSI implementation of the native module that can be called directly from JavaScript without going through the React Native Bridge.