乐于分享
好东西不私藏

用 AI 快速写一个 APP!RN Expo 54版本完整避坑教程.

用 AI 快速写一个 APP!RN Expo 54版本完整避坑教程.
Hi,我是蓝莓,最近在用AI写APP的时候。
发现React Expo的踩坑点。今天总结5点,主要有标签,css,路由跳转,本地存储,移动端特殊的设计(安全区域,判断设备等)
流程是:先让AI实现数据,然后实现基本的UI和CSS。然后实现交互。
注意下载第三方库的时候:一定要用 npx expo install xxx别用 npm install,它会自动挑跟你 Expo SDK 54 兼容的版本。
第一坑:标签坑:
推荐react-native-render-html,使用原生标签即可。
文字错误写法:<View>Hello</View>正确写法:<View><Text>Hello</Text></View>
按钮错误写法:<buttononClick={}></button>正确写法:<TouchableOpacityonPress={() => alert('')}>  <Text >按钮</Text></TouchableOpacity><Button></Button>系统原生按钮(样式很丑,一般不用)
可滚动区域:正确写法:<ScrollView></ScrollView>输入框:正确写法:<TextInputplaceholder="请输入"/>图片:正确写法:<Imagesource={{uri:"https://..."  }}/>
第二坑:CSS坑
没有.css文件。全员FlexBox。默认纵向布局。样式全是用 JS 对象写的。
没有 width: '100%' 这种字符串值,直接写数字100(数字 = 密度像素 dp
没有 :hover 之类的伪类。用 style={[styles.a, isActive && styles.b]}
错误写法.box { display: flex; font-size: 14px; }正确写法:const styles = StyleSheet.create({  box: {    flex1,    justifyContent'center',    alignItems'center',    backgroundColor'#fff' // 手机里没有 background,必须写 backgroundColor  },  text: {    fontSize14, // 手机里不需要写 px,直接写数字    color'#333'  }});推荐twrnc库:import tw from "twrnc";<View style={tw`p-4 bg-white rounded-xl`}>使用NativeWind库,可以向tailwind一样写:<View className="p-4 bg-white">NativeWind v4对应:Tailwind v3NativeWind v5对应:Tailwind v4pnpm add nativewind配置这三个:babel.config.jstailwind.config.jsmetro.config.js
第三坑:没有标签跳转,没有打开网页。需要下载库。
Expo 54 默认用 expo-router
像 Next.js 一样按文件名自动生成路由,不用手写路由表。
错误写法<a href="">history.push();正确写法:import { useLocalSearchParams } from 'expo-router';const { id } = useLocalSearchParams();下载:expo-web-browser  npx expo install expo-web-browserexpo-router npx expo install expo-router
目录如下:
app/                          ← 路由根目录├── _layout.js                ← 全局布局(必须)├── index.js                  ← / 首页├── about.js                  ← /about 关于页├── profile.js                ← /profile 个人中心├── details/│   └── [id].js               ← /details/123 动态参数└── (tabs)/                   ← 分组文件夹,不影响 URL    ├── _layout.js            ← Tab 栏布局    ├── home.js               ← tab1    └── me.js                 ← tab2
第四坑:手机特有功能:必须要“授权”
页面安全区域
npx expo install react-native-safe-area-contextimport { SafeAreaProviderSafeAreaView } from 'react-native-safe-area-context';<SafeAreaView></SafeAreaView>
判断是安卓还是苹果
import { Platform } from 'react-native';const isIOS = Platform.OS === 'ios'// 返回 true 或 false
图标:
npx expo install @expo/vector-icons
UI库:
npx expo install react-native-paper或者 NativeBasenpx expo install native-base
获取定位 一定要先请求:
npx expo install expo-location// 1. 请求权限const { status } = await Location.requestForegroundPermissionsAsync();if (status !== 'granted') {   setErrorMsg('权限被拒绝');   return;}// 2. 获取当前位置const locationData = await Location.getCurrentPositionAsync({  accuracyLocation.Accuracy.High// 定位精度});setLocation(locationData);
第五点:本地存储:
import AsyncStorage from '@react-native-async-storage/async-storage';// 存await AsyncStorage.setItem('token''abc123');// 取const token = await AsyncStorage.getItem('token');// 删await AsyncStorage.removeItem('token');
网络请求:
useEffect(() => {  fetch('https://api.example.com/data')    .then(res => res.json())    .then(data => console.log(data))    .catch(err => console.error(err));}, []);// 推荐用 async/awaitconst loadData = async () => {  try {    const res = await fetch('https://api.example.com/data');    const data = await res.json();    setList(data);  } catch (e) {    console.error(e);  }};Hooks 和 React 一模一样

Hooks 和 React 一模一样

附上一个安装:NativeWind库的教程。

https://juejin.cn/post/7501205056948011044

可能遇到的bug:

https://github.com/nativewind/nativewind/issues/1716