[英]Can I use the same codebase for both a React Native macOS app and a React Native Windows app?
I am trying to create an app using React Native that runs on iOS, Android, macOS and Windows.我正在尝试使用在 iOS、Android、macOS 和 Windows 上运行的 React Native 创建一个应用程序。 An advantage of being able to use React Native is that you can share code between platforms.能够使用 React Native 的一个优势是您可以在平台之间共享代码。
I am following the setup guides for React Native macOS and Windows , but in the guides, they guide you through creating a new app from scratch for each platform.我正在遵循 React Native macOS和Windows的设置指南,但在指南中,它们指导您从头开始为每个平台创建一个新应用程序。 As I want to use the same app, so I don't have to write the same code twice, I create the macOS app following the guide.因为我想使用同一个应用程序,所以我不必两次编写相同的代码,所以我按照指南创建了 macOS 应用程序。 Then, in this app, I use the command npx react-native-windows-init --overwrite
from the Windows guide to initialise the Windows platform.然后,在这个应用程序中,我使用 Windows 指南中的命令npx react-native-windows-init --overwrite
来初始化 Windows 平台。 But when I then go to run the macOS app using the command npx react-native run-macos
, I get the error:但是,当我使用命令npx react-native run-macos
去运行 macOS 应用程序时,出现错误:
Cannot find module 'metro-config/src/defaults/exclusionList'找不到模块“metro-config/src/defaults/exclusionList”
and和
One thing I find strange about the setup guides is that for macOS it tells you to use React Native 0.63 and for Windows 0.66.我对安装指南感到奇怪的一件事是,对于 macOS,它告诉您使用 React Native 0.63 和 Windows 0.66。 I used 0.66 for both, maybe this is the problem.我对两者都使用了 0.66,也许这就是问题所在。 But if it is, and each platform requires different versions of React Native, how can I use the one app / codebase for both platforms?但如果是这样,并且每个平台都需要不同版本的 React Native,我该如何为两个平台使用同一个应用程序/代码库? This goes against one of React Native's core premises:这违背了 React Native 的核心前提之一:
Create platform-specific versions of components so a single codebase can share code across platforms.创建特定于平台的组件版本,以便单个代码库可以跨平台共享代码。
Is it possible to create a single app for both desktop platforms like you would with iOS and Android, and if so, how?是否可以像使用 iOS 和 Android 一样为两个桌面平台创建单个应用程序,如果可以,如何创建?
Yes, you can.是的你可以。 You will have to create a mono repo Or you can use mono repo boilerplate.您将必须创建一个 mono 回购或者您可以使用 mono 回购样板。 Here is a link to it https://github.com/mmazzarolo/react-native-universal-monorepo这是它的链接https://github.com/mmazzarolo/react-native-universal-monorepo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.