使用EXPO套件在React Native中加载自定义本机组件 [英] Load custom native component in react native using expo kit

查看:127
本文介绍了使用EXPO套件在React Native中加载自定义本机组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载自定义Android WebView,以便能够使用html文件输入上传文件(默认情况下,Android Webview无法与输入文件配合使用). 我正在使用此代码,唯一的区别是我使用的是expo工具包,所以我的MainApplication.java是不同的(默认情况下继承自另一个类):

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);
  }
}

基本上,git代码的作用是覆盖默认的响应本机Webview,以使其在Android中使用CustomWebView.java,并使用带有此代码的requireNativeComponent(

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,
},

});

但是当我使用exp start运行应用程序并导航到具有CustomWebView的屏幕时,我收到此错误:

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

总结问题是,React Native无法读取我的自定义本机组件.有人可以帮我吗?

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

推荐答案

Expo默认情况下不支持任何自定义本机模块.这是因为它们只有一个perbuilt二进制文件,并且仅加载您编写的JS包.因此,用Expo编写的任何代码都只能是纯Javascript. 但是Expo文档确实说您可以在分离后添加自定义本机模块. 这里有更多信息:

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/版本/latest/guides/detach.html#should-i-detach

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

这篇关于使用EXPO套件在React Native中加载自定义本机组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆