使用expo工具包在native native中加载自定义本机组件

时间:2017-12-29 01:08:53

标签: android reactjs react-native react-native-android expo

我正在尝试加载自定义Android WebView,以便能够使用html文件输入上传文件(默认情况下,Android webview不能使用输入文件)。 Im using this code,唯一的区别是我使用expo工具包,所以我的MainApplication.java是不同的(默认情况下继承自另一个类):

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,使用带有此代码的this is on CustomWebView.android.js}的requireNativeComponent:

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

});

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

enter image description here

总结问题是React Native没有读取我的自定义本机组件。有人能帮帮我吗?

5 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:7)

使用expo时不能使用本机代码,只能使用他们提供的内容。弹出将允许你使用本机代码但是你不能再使用expo了,我很确定它是不可逆转的。

您可以在此处详细了解使用expo的注意事项:https://facebook.github.io/react-native/docs/getting-started.html#caveats

答案 2 :(得分:5)

您可以从ignite-cli的最佳代码生成器开始,称为Ignite它很容易使用它有一些BoilerPlates

从给定的有用样板文件中,您可以使用Ignite Expo BoilerPlate来设置所有具有expo + native modules支持的代码。将ignite new [AppName] -b ignite-expo安装为全局包并运行{{1}}

之后

所以这对你来说是一个轻松的步骤。此外,它还添加了一些有用的其他模块。

答案 3 :(得分:1)

弹出项目后,尝试通过template<typename T> constexpr bool foo1(T &) { return std::is_const<T>::value; } template<typename T> T foo2(T &); int main() { int const x = 0; constexpr bool a = foo1(x); constexpr bool b = std::is_const<decltype(foo2(x))>::value; } 使用它将无法获得您希望实现的结果。虽然仍然支持expo kit,但您现在需要新的本机代码,因此您需要使用exp start运行它。当你这样做时,它不仅会像react-native run-android那样为JS提供服务,而且还会编译你的本机代码并将其放入你的设备中。

现在,使用exp start服务于JS捆绑包,但是捆绑包找不到您的原生模块,因为它在通用的expo客户端中不存在。

答案 4 :(得分:0)

如先前的回答所述,expo和本机模块无法并驾齐驱。 由于expo仅提供编辑javascript代码的功能,而没有提供本机模块的功能,因此可以将本机模块视为expo中的黑盒。

但是,如果您试图将本机模块集成到expo中,或者尝试同时运行expo和react-native本机模块。这是一本非常有趣的读物https://codersera.com/blog/running-expo-react-native-together/