Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use turbo module within java code
import com.facebook.react.ReactApplication ;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.ReactPackage;
import com.facebook.react.TurboReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.shell.MainReactPackage;
public class MainApplication implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
protected String getJSBundleFile() {
return CodePush.getJSBundleFile();
}
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new TurboReactPackage() {
@Override
public abstract NativeModule getModule(String name, ReactApplicationContext : reactContext) {
switch(name) {
case MyCustomPackage.MODULE_NAME :
return MyCustomPackage(reactContext);
default :
throw IllegalArgumentException("Could not find module $name")
}
}
@Override
public abstract ReactModuleInfoProvider getReactModuleInfoProvider {
return ReactModuleInfoProvider {
Map<String, ReactModuleInfo> map = HashMap<String, ReactModuleInfo>() ;
map.put(MyCustomPackage.MODULE_NAME, new ReactModuleInfo(MyCustomPackage.MODULE_NAME, "com.mypackagename.MyCustomPackage", false, false, false, false, true));
return map; } ;
}
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
}
}
@wgarrido

This comment has been minimized.

Copy link

wgarrido commented Nov 6, 2019

Your code is not working.
It's half transform in Java.

new TurboReactPackage() {
                @Override
                public NativeModule getModule(String name, ReactApplicationContext reactContext) {
                  switch(name) {
                    case MyCustomPackage.REACT_CLASS :
                      return new MyCustomPackage(reactContext);
                    default :
                      throw new IllegalArgumentException("Could not find module $name");
                  }
                }

                @Override
                public ReactModuleInfoProvider getReactModuleInfoProvider() {
                  return new ReactModuleInfoProvider() {
                    @Override
                    public Map<String, ReactModuleInfo> getReactModuleInfos() {
                      Map<String, ReactModuleInfo> map = new HashMap<String, ReactModuleInfo>() ;
                      map.put(MyCustomPackage.REACT_CLASS, new ReactModuleInfo(MyCustomPackage.REACT_CLASS, "com.mypackagename.MyCustomPackage", false, false, false, false, true));
                      return map;
                    }
                  };

                }
              }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.