关于 react-native-basics
react-native-basics 是面向 React Native 应用开发者的学习技能,涵盖核心组件、样式、Flexbox、平台专属代码和 Expo 设置,并提供示例与常见问题排查指引。
主要功能
- 01React Native 核心组件
- 02使用 StyleSheet 设置样式
- 03通过 Flexbox 构建响应式布局
- 04编写 iOS 和 Android 专属代码
- 05配置并使用 Expo
适用场景
- 01学习 React Native 基础知识
- 02使用 Flexbox 构建布局
- 03处理平台专属样式
- 04通过 Expo 启动应用
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-react-native --skill react-native-basics安装后在 Claude Code 中输入/react-native-basics确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/config.yamlschema.jsonreferences/GUIDE.mdPATTERNS.mdscripts/validate.py详细介绍
React Native 基础技能
学习可用于生产环境的 React Native 基础知识,包括核心组件、StyleSheet、Flexbox 和 Expo SDK。
前置条件
- JavaScript/TypeScript 基础知识
- React 基础知识(组件、props、state、hooks)
- 已安装 Node.js 和 npm/yarn
- Xcode(iOS)或 Android Studio(Android)
学习目标
完成此技能后,你将能够:
- 创建并组合 React Native 组件
- 使用 StyleSheet API 应用样式
- 使用 Flexbox 构建响应式布局
- 使用特定平台的代码(iOS/Android)
- 设置并使用 Expo 进行开发
涵盖主题
1. 核心组件
View - Container component (like div)
Text - Text display (required for strings)
Image - Image rendering
ScrollView - Scrollable container
FlatList - Optimized list rendering
TextInput - User input
Pressable - Touch handling
2. 样式系统
// StyleSheet.create for performance
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
padding: 16,
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#1a1a1a',
},
});
3. Flexbox 布局
// Common patterns
flexDirection: 'row' | 'column' // Main axis
justifyContent: 'center' | 'space-between' // Main axis alignment
alignItems: 'center' | 'stretch' // Cross axis alignment
flex: 1 // Grow to fill
4. 特定平台的代码
import { Platform } from 'react-native';
// Method 1: Platform.select
const styles = StyleSheet.create({
shadow: Platform.select({
ios: { shadowColor: '#000', shadowOpacity: 0.1 },
android: { elevation: 4 },
}),
});
// Method 2: Platform.OS
if (Platform.OS === 'ios') {
// iOS specific code
}
5. Expo 设置
# Create new project
npx create-expo-app MyApp
cd MyApp
# Start development
npx expo start
# Build for production
eas build --platform all
快速入门示例
import React from 'react';
import { View, Text, StyleSheet, FlatList, Pressable } from 'react-native';
interface Item {
id: string;
title: string;
}
export default function App() {
const [items, setItems] = React.useState<Item[]>([
{ id: '1', title: 'Learn React Native' },
{ id: '2', title: 'Build first app' },
]);
return (
<View style={styles.container}>
<Text style={styles.title}>My Tasks</Text>
<FlatList
data={items}
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<Pressable style={styles.item}>
<Text>{item.title}</Text>
</Pressable>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 16, backgroundColor: '#f5f5f5' },
title: { fontSize: 24, fontWeight: 'bold', marginBottom: 16 },
item: { backgroundColor: '#fff', padding: 16, marginBottom: 8, borderRadius: 8 },
});
常见错误与解决方案
| 错误 | 原因 | 解决方案 |
|---|---|---|
| "Text strings must be wrapped" | <Text> 外的原始文本 | 将所有文本放在 <Text> 中 |
| 样式未生效 | 使用普通对象而非 StyleSheet | 使用 StyleSheet.create() |
| FlatList 无法滚动 | 没有设置高度或 flex | 为父级添加 flex: 1 |
| 图片未显示 | source 无效 | 检查 URI 或 require 路径 |
验证清单
- 组件能够无错误地渲染
- 样式在两个平台上都能正确应用
- FlatList 能够高效处理大型列表
- 特定平台的代码能够正常工作
- Expo 构建成功
相关资源
用法
Skill("react-native-basics")
Bonded Agent:01-react-native-fundamentals
评论
支持 Markdown