正如其他人所提到的,没有快速的方法可以将 react 转换为 react-native。
如果您希望您的 React 应用程序在移动设备上运行而无需重写您的代码库,一个可能的替代方案是使用 Cordova。为了好玩,我在几分钟内使用 Cordova 将 react-web 应用程序移植到移动应用程序中。这种方法有一些缺点,但好处是您可以在很短的时间内拥有一个可以运行的移动应用程序。
如果有人对 Cordova 替代方案感兴趣,请执行以下步骤:
REACT SETUP(在命令行中完成)
>1. npx create-react-app my-app
>2. cd my-app
>3. npm start
部署到静态:
>1. Update your package.json file from your my-app directory to add "homepage":"." (see below)
"name": "my-app",
"version": "0.1.0",
"private": true,
"homepage":".",
"dependencies": {
>2. Build (in command line) npm run build
CORDOVA 集成(在命令行中)
>1. cd ../ (change to wherever you want the project created, don't do this inside your existing react app folder)
>2. npm install -g cordova (only if you already don't have Cordova installed)
>3. cordova create MyApp
>4. cd MyApp
>5. cordova platform add ios //or android or browser
添加包含 React 项目的几个步骤
>1. Open your Cordova MyApp www folder, delete all files and folders in it except for the 'js' folder
>2. Back in your react build folder update the index file to add two cordova scripts:
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
>3. copy all your files from the react build folder into the Cordova www folder (replacing everything except the js folder)
构建 REACT-CORDOVA APP(在命令行中,在您的 Cordova 项目中)
>1. cordova build ios //or android or browser
测试或部署到存储等。
>1. Use xcode to open open your react-cordova .xcodeproject, this can be found in the MyApp/Platforms/ios/
>2. Select your simulator and run. Enjoy your new mobile app!
TWEAK 您需要做一些小的调整(例如删除点击延迟...等)-我认为这就是人们认为 Cordova 应用程序很慢的原因,但它很容易解决...