当前位置:   article > 正文

android studio创建react native项目_android studio rn项目

android studio rn项目

前言:react native官网上少给了一个命令,导致各种报错,下面给出全部命令,亲测有效


一、npm方式(速度很慢)

  1. cnpm uninstall -g react-native-cli @react-native-community/cli // 清除脚手架
  2. cnpm i react-native-cli -g // 创建脚手架
  3. npx react-native init AwesomeProject --version 0.68.2 // 初始化AwesomeProject工程
  4. npx react-native run-android // 启动项目

二、 yarn方式(快速,需要JDK17)

  1. npm install -g yarn // 安装yarn
  2. yarn add global react-native
  3. yarn add global react-native-cli
  4. react-native init sample

中途会遇到gradle下载很慢的情况,现在本地下载好然后放到工程里

百度网盘下载地址:https://blog.csdn.net/ii950606/article/details/109105402 

三、安装react-navigation

  1. yarn add @react-navigation/native // 安装核心包
  2. yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view // 安装依赖

该控件包含的3个组件均需要单独安装

1)Stack

安装命令: 

  1. yarn add @react-navigation/stack // Stack路由栈导航

2)Drawer

安装命令

yarn add @react-navigation/drawer // //Drawer抽屉导航

代码实例

  1. import * as React from 'react';
  2. import { View, Text } from 'react-native';
  3. import { NavigationContainer } from '@react-navigation/native';
  4. import { createDrawerNavigator } from '@react-navigation/drawer';
  5. function Feed() {
  6. return (
  7. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  8. <Text>Feed Screen</Text>
  9. </View>
  10. );
  11. }
  12. function Article() {
  13. return (
  14. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  15. <Text>Article Screen</Text>
  16. </View>
  17. );
  18. }
  19. const Drawer = createDrawerNavigator();
  20. function MyDrawer() {
  21. return (
  22. <Drawer.Navigator>
  23. <Drawer.Screen name="Feed" component={Feed} />
  24. <Drawer.Screen name="Article" component={Article} />
  25. </Drawer.Navigator>
  26. );
  27. }
  28. export default function App() {
  29. return (
  30. <NavigationContainer>
  31. <MyDrawer />
  32. </NavigationContainer>
  33. );
  34. }

报错解决

启动过程中可能报reanimated is not a worklet错误,找到babel.config.js,添加如下代码
  1. module.exports = {
  2. plugins: ['react-native-reanimated/plugin'],
  3. };

 执行清空缓存命令

yarn start --reset-cache 

3)Tab

安装命令

yarn add @react-navigation/bottom-tabs // Tab导航

代码实例

  1. import * as React from 'react';
  2. import { Text, View } from 'react-native';
  3. import { NavigationContainer } from '@react-navigation/native';
  4. import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
  5. function HomeScreen() {
  6. return (
  7. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  8. <Text>Home!</Text>
  9. </View>
  10. );
  11. }
  12. function SettingsScreen() {
  13. return (
  14. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  15. <Text>Settings!</Text>
  16. </View>
  17. );
  18. }
  19. const Tab = createBottomTabNavigator();
  20. export default function App() {
  21. return (
  22. <NavigationContainer>
  23. <Tab.Navigator>
  24. <Tab.Screen name="Home" component={HomeScreen} />
  25. <Tab.Screen name="Settings" component={SettingsScreen} />
  26. </Tab.Navigator>
  27. </NavigationContainer>
  28. );
  29. }

 报错解决(启动过程中可能报ndk错误,按下图方式勾选下载解决)


总结

坑爹的官网,创建脚手架命令没给,直接跳过,你这让小白们情何以堪,各种报错,白白浪费时间。即使用yarn还是会遭遇各种问题,还好最终都能解决。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/543832
推荐阅读
相关标签
  

闽ICP备14008679号