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

时间:2016-05-01 20:33:47

标签: reactjs react-native

是否有可能使用加载和缓存远程反应本机组件在本地反应?

我们希望能够构建和部署UI的各个部分以及各自的后端微服务,以便我们可以配置客户端集中使用哪个组件/后端组合。

1 个答案:

答案 0 :(得分:0)

在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

如您所见,它已经支持远程和本地加载捆绑包。出于性能原因,绝对不建议在生产中远程加载它。

您可以查看www.apphub.ioCodePush等工具。它们可以帮助您动态推送更新。

虽然没有其他选择解决了此处提出的问题:如何动态加载单个组件。由于您可以在JS中远程加载和评估资源,并且只需要返回使用有效的反应本机组件的有效JSX,这绝对不是不可能的!

相关问题