Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load custom native component in react native using expo kit

Im trying to load a custom Android WebView to be able to upload files using html file inputs (by default Android webview wont work with input file). Im using this code, the only difference is that im using expo kit, so my MainApplication.java is different (inherits from another class by default):

public class MainApplication extends MultiDexApplication {

    // Needed for `react-native link`
    public List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
        new CustomWebViewPackage()
    );
  }

  @Override
  public void onCreate() {
      super.onCreate();
      SoLoader.init(this, /* native exopackage */ false);
  }
}

Basically what the git code do is override the default react native webview to make it use the CustomWebView.java in Android, using requireNativeComponent with this code (this is on CustomWebView.android.js):

var RCTWebView = requireNativeComponent('CustomWebView', WebView, {
nativeOnly: {
    messagingEnabled: PropTypes.bool,
},

});

But when i run the application using exp start and navigate to the screen that has the CustomWebView i receive this error:

enter image description here

Summarizing the problem is that my custom native component is not being read by React Native. Can someone help me please?

like image 275
Renato Probst Avatar asked Dec 29 '17 01:12

Renato Probst


People also ask

How do I add custom native code to my Expo project?

The tradeoff is that Expo Go does not allow you to add custom native code, you can only use native modules built into the Expo SDK. There are many great libraries available outside of the Expo SDK, and you may even want to build your own native library.

Can I use Expo package in React Native?

If you create a bare React Native project through expo-cli by running expo init and choosing a Bare template, your project will have react-native-unimodules installed and configured for you. You can run this project by using react-native run-ios or react-native run-android rather than expo start .

Can I do Native changes in case of Expo?

Make native changesYou can do whatever you want in the Xcode and Android Studio projects. To add third-party native modules for React Native, non-Expo-specific instructions such as react-native link should be supported. Read more details about changing native dependencies in your ExpoKit project.


1 Answers

Expo by default will not support any custom native modules. This is because they have a single perbuilt binary and they only load the JS bundle that you write. So any code you write with Expo can only be pure Javascript. But Expo documentation does say that you can add custom native modules after detaching. More info here:

https://docs.expo.io/versions/latest/guides/detach.html#should-i-detach

https://docs.expo.io/versions/latest/introduction/faq.html#what-is-the-difference-between-expo-and-react-native

https://github.com/expo/expo/issues/56

like image 107
Atul Avatar answered Sep 24 '22 22:09

Atul