跳到正文
Skillpedia
移动应用280k · #236

react-native-patterns

它能做什么

本指南介绍如何使用 React Native 和 Expo 组织并构建移动应用。它展示了导航、数据管理、列表展示、样式设置以及调用手机功能等方面的最佳实践。

适用场景

  • ✓在 React Native 应用中创建新页面或组件时。
  • ✓设置用户在应用页面之间跳转的方式时。
  • ✓决定如何在应用中存储和获取信息时。

技能链接

https://github.com/affaan-m/ECC/blob/main/pi/core/skills/react-native-patterns/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/affaan-m/ECC/blob/main/pi/core/skills/react-native-patterns/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我该如何使用 Expo Router 组织 React Native 应用各页面之间的导航?”

每周 5 个新技能,1 分钟读完。

工作原理

指南建议使用 Expo Router,通过文件来组织页面,文件夹结构即决定导航方式。指南还讲解了如何区分不同类型的数据(来自服务器、客户端和表单),以及如何借助 TanStack Query 等库高效地获取数据。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

React Native / Expo Patterns

Practical patterns for building production React Native apps with Expo. Covers navigation, state, data fetching, lists, styling, and native APIs. Pairs with the rules/react-native/ ruleset: rules say what to enforce, this skill shows how.

Libraries named below (NativeWind, Zustand/Jotai, TanStack Query) are common, well-established options shown for illustration — the patterns matter more than the specific package, and any equivalent works. Zod is used for validation to stay consistent with ECC's existing typescript/ rules.

These patterns assume the managed Expo workflow (Expo Router, EAS, expo-* modules) on the New Architecture (the default in recent Expo SDKs, mandatory from SDK 55+). They do NOT assume the browser DOM — React Native has no <div>, no URL bar, and no web data-fetching defaults.

When to Activate

Use this skill when:

  • Building or editing React Native / Expo screens, components, or navigation
  • Setting up routing with Expo Router (file-based app/ directory)
  • Deciding where state belongs (server cache vs client store vs route params vs form)
  • Wiring data fetching with TanStack Query and validating responses with Zod
  • Rendering long or heavy lists
  • Choosing or applying a styling approach (NativeWind or StyleSheet)
  • Accessing native device APIs (camera, location, notifications) or secure storage
  • Reviewing RN code for mobile-specific issues

Do NOT use the web/React-DOM patterns here — URL-as-state, <div>, and SWR-for-browser do not apply to React Native.

Core Concepts

Project structure (Expo Router)

File-based routing under app/. Keep route files thin: they read and validate params, then delegate to a screen component that lives in components/ or features/.

app/
  _layout.tsx          # root stack
  (tabs)/
    _layout.tsx        # tab navigator
    index.tsx          # Home
  user/[id].tsx        # dynamic route
components/
features/
  user/UserProfile.tsx

Navigation: validate route params

Deep links and dynamic routes deliver untrusted strings. Validate them with Zod before use.

// app/user/[id].tsx
import { useLocalSearchParams, router } from 'expo-router'
import { z } from 'zod'
import { UserProfile } from '@/features/user/UserProfile'

const Params = z.object({ id: z.string().uuid() })

export default function UserRoute() {
  const parsed = Params.safeParse(useLocalSearchParams())
  if (!parsed.success) {
    router.replace('/not-found')
    return null
  }
  return <UserProfile userId={parsed.data.id} />
}

State: keep concerns separate

Do not duplicate server data into a client store. Each concern has its own home.

Concern Common choices
Server state (remote data) a server-cache library (TanStack Query, SWR)
Client/UI state a lightweight store (Zustand, Jotai) or Context
Route/navigation state Expo Router params
Form state a form library (e.g. React Hook Form) + schema validation
Secrets / tokens expo-secure-store
Non-secret persistence AsyncStorage / MMKV

Prefer local useState until state genuinely needs sharing.

Data fetching: a cache library + Zod

Use a server-cache library (TanStack Query, SWR) instead of fetch-in-useEffect. Validate at the boundary and infer types from the schema. Handle loading, error, and empty states explicitly. (Example uses TanStack Query.)

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { z } from 'zod'

const User = z.object({ id: z.string(), email: z.string().email() })
type User = z.infer<typeof User>

export function useUser(id: string) {
  return useQuery({
    queryKey: ['user', id],
    queryFn: async (): Promise<User> => User.parse(await api.getUser(id)),
  })
}

export function useUpdateEmail(id: string) {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: (email: string) => api.updateEmail(id, email),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['user', id] }),
  })
}

Lists: virtualize, never map a big array in a ScrollView

import { FlatList } from 'react-native'

<FlatList
  data={items}
  keyExtractor={(item) => item.id}
  renderItem={renderItem}          // memoized
  initialNumToRender={10}
  windowSize={5}
/>

Use FlashList (Shopify) for large or heterogeneous lists.

Styling: pick one system

…

常见问题

什么是 Expo Router?

它是一种基于文件的方式,用来组织 React Native 应用的页面,文件夹结构决定了导航方式。

为什么要区分数据类型?

区分数据有助于让代码结构更清晰,也能避免服务器上的信息在应用中被不必要地重复存放。

如何高效地展示很长的列表?

指南推荐使用“虚拟化”技术,比如使用 FlatList 这类组件。这样即使要展示大量条目,应用也不会变慢。

详情

作者的原始描述

React Native and Expo app patterns — Expo Router navigation, state separation (server/client/route/form), TanStack Query data fetching with Zod, performant lists, NativeWind/StyleSheet styling, native APIs, and secure storage. Use when building or editing React Native / Expo screens, components, navigation, or data layers.

repo: affaan-m/ECC

热度

GitHub 星标
280k
排名
#236
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能