简体   繁体   English

Load从url动态地响应本机组件

[英]Load react native components dynamically from a url

Is it possible to use load and cache remote react native components on the fly in react native? 是否有可能使用加载和缓存远程响应本机组件动态反应原生?

We want to be able to build and deploy parts of the UI coupled with their respective back-end microservices so that we can configure what clients use what component/back-end combo centrally. 我们希望能够构建和部署UI的各个部分以及它们各自的后端微服务,以便我们可以配置客户端集中使用哪个组件/后端组合。

Open your ios project in xcode, and check out AppDelegate.m. 在xcode中打开你的ios项目,然后查看AppDelegate.m。

NSURL *jsCodeLocation;
#ifdef DEBUG
    /**
     * Loading JavaScript code - uncomment the one you want.
     *
     * OPTION 1
     * Load from development server. Start the server from the repository root:
     *
     * $ npm start
     *
     * To run on device, change `localhost` to the IP address of your computer
     * (you can get this by typing `ifconfig` into the terminal and selecting the
     * `inet` value under `en0:`) and make sure your computer and iOS device are
     * on the same Wi-Fi network.
     */
    jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/compiled/src/index.ios.bundle?platform=ios&dev=true"];
  #else
    /**
     * OPTION 2
     * Load from pre-bundled file on disk. The static bundle is automatically
     * generated by the "Bundle React Native code and images" build step when
     * running the project on an actual device or running the project on the
     * simulator in the "Release" build configuration.
     */
    jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  #endif

As you see, it already supports loading the bundle either remotely and locally. 如您所见,它已经支持远程和本地加载捆绑包。 Loading it remotely is definitely not recommended in production for performance reasons. 出于性能原因,绝对不建议在生产中远程加载它。

You can check out tools like www.apphub.io and CodePush . 您可以查看www.apphub.ioCodePush等工具。 They help you push out updates dynamically. 它们可以帮助您动态推出更新。

Although none of the alternatives solves the problem asked here: How to load single components dynamically. 虽然没有其他选择解决了这里提出的问题:如何动态加载单个组件。 As you are free to load and evaluate resources remotely in JS, and only need to return valid JSX that uses valid react native components, it is definitely not impossible! 由于您可以自由地在JS中远程加载和评估资源,并且只需要返回使用有效的反应本机组件的有效JSX,这绝对不是不可能的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM