React Native中的多租户 [英] Multi tenancy in React Native

查看:124
本文介绍了React Native中的多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在React native中支持多租户?就像使用相同代码库的多个版本一样.

How we can support multi tenancy in React native ? Like multiple builds with same codebase.

  • 在iOS中,我们可以为多个目标提供支持,以使用相同的代码库支持多个构建.
  • 对于android,我们可以为此目的提供gradle配置

但不确定如何在本机React中实现这一目标.

but not sure how we can achieve this in react native.

推荐答案

使用React Native时,iOS和Android项目都属于项目结构的一部分.从某种意义上来说,它们是正常的" XCode和Android项目,因为React包含在库中.

When working with React Native you have as part of your project structure both the iOS and the Android projects. These are "normal" XCode and Android projects in the sense that there's nothing special about them, React is included as a library.

因此,您可以使用XCode打开由React Native CLI生成的项目,并照常在此处定义构建目标.作为参考,您可以在此处看到他们使用其cli在iOS上运行项目的nodejs脚本: https://github.com/facebook/react -native/blob/master/local-cli/runIOS/runIOS.js 如果您需要做一些更复杂的事情,则始终可以将该代码用作参考并自己调用xcodebuild/xcrun.或者简单地,您可以直接从XCode运行ReactNative项目.

So you can open the project generated by the React Native CLI with XCode and define there your build targets as usual. For reference, here you can see the nodejs script they use to run the project on iOS using their cli: https://github.com/facebook/react-native/blob/master/local-cli/runIOS/runIOS.js If you need to do something more sophisticated, you could always just use that code as a reference and call xcodebuild/xcrun yourself. Or simply you can just run the ReactNative project from XCode directly.

关于Android,在React Native项目的"android"目录中,您会找到几个.gradle文件,您可以再次调整这些文件.

Regarding Android, inside the "android" directory of your React Native project you'll find a couple of .gradle files which again you can tweak to your needs.

由于您的本机项目结构的ios和android文件夹通常都受版本控制,因此您可以轻松跟踪这些配置.

Since both the ios and android folders of your react native project structure are normally kept under version control you can easily keep track of these configurations.

我认为这种方法与Cordova/ionic所建议的方法不同,后者通常会忽略本地项目,并通过cli按需(例如,在CI上)重新创建本地项目.

I think this approach is different than the one suggested by Cordova/ionic, where the native projects are normally ignored and recreated on demand (e.g. on CI) through their cli.

澄清后已更新

如果您针对该应用的每个版本具有不同的配置,那么您必须使用的选项是这样的:

If you have different configs specific for each version of the app, then one option you have is to use something like this: https://www.npmjs.com/package/react-native-config that lets you specify and import configs and access them pretty much anywhere, js and native side.

如果您具有不同的逻辑和代码,而不仅仅是简单的配置,那么一个选择是将应用程序组件拆分为不同的子文件夹,然后使用一些命名约定为该版本动态导入特定组件.

If you have different logic and code, and not just simple configs, one options is to split you app components into different subfolders and then use some naming convention to dynamically import specific components for that version.

例如,如果每个客户都有一个不同的HomePage组件,则可以创建一个customer1/Home.js文件夹,并使用

For example, if you have a HomePage component different for every customer, you could create a customer1/Home.js folder and dynamically import that component with

const Home = require('./' + Config.customerName + '/Home.js')

您还可以检查是否随后定义了Home,然后回退并要求使用默认实现.

You could also check if Home is then defined, and fall back and require instead a default implementation.

如果代码版本在不同版本之间存在很大差异,我认为您应该考虑采用另一种方法:与其构建一个版本来统治所有版本",还可以将共享组件和逻辑提取到单独的npm包中,并具有独立的功能.应用的版本和内部版本,并参考通用软件包.

If the code base diverges a lot between the different versions, I think you should consider a different approach: instead of building "one version to rule them all" you can extract shared components and logic into a separate npm package, have independent versions and builds for the apps and reference the common package.

根据您的情况,从长远来看,这种方法可能会更简单,因为如果您确实需要(例如,一个疯狂的要求的客户),您将有更多自由与默认实现区别开来,并且结构会更加模块化,而不必使所有事情复杂化.

Depending on you scenario, this approach could be simpler in the long run, as you would have more freedom to diverge from the default implementation if you really need too (e.g. a client with crazy requirements) and the structure would be more modular, without having to complicate everything.

希望我对此有所帮助,显然这没有单一的方法或通用的解决方案,因为这很大程度上取决于您的情况.

Hope my take on this can be helpful, obviously there's no single approach or universal solution to this, as it's very dependent on your scenario.

这篇关于React Native中的多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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