--- name: react-native-patterns description: 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. origin: ECC --- # 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 `
`, 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, `
`, 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. ```tsx // 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 } ``` ### 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.) ```tsx 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 export function useUser(id: string) { return useQuery({ queryKey: ['user', id], queryFn: async (): Promise => 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 ```tsx import { FlatList } from 'react-native' item.id} renderItem={renderItem} // memoized initialNumToRender={10} windowSize={5} /> ``` Use `FlashList` (Shopify) for large or heterogeneous lists. ### Styling: pick one system `StyleSheet.create()` is the framework-native option; utility-class libraries (e.g. NativeWind) are a common alternative. Choose one and stay consistent. Never build style objects inline in JSX on hot paths. ```tsx // NativeWind Hello // StyleSheet const styles = StyleSheet.create({ card: { padding: 16, borderRadius: 16, backgroundColor: '#fff' } }) ... ``` ### Native APIs: wrap in hooks, clean up effects Keep Expo SDK calls and subscriptions inside `use*` hooks, not in JSX. Always clean up. ```tsx import { useEffect, useState } from 'react' import * as Location from 'expo-location' type LocationState = | { status: 'loading' } | { status: 'denied' } | { status: 'granted'; coords: Location.LocationObjectCoords } export function useCurrentLocation() { // Track status, not just coords — so the UI can tell "still loading" apart // from "permission denied" and show an actionable message. const [state, setState] = useState({ status: 'loading' }) useEffect(() => { let active = true ;(async () => { const { status } = await Location.requestForegroundPermissionsAsync() if (status !== 'granted') { if (active) setState({ status: 'denied' }) return } const pos = await Location.getCurrentPositionAsync({}) if (active) setState({ status: 'granted', coords: pos.coords }) })() return () => { active = false } // ignore stale result after unmount }, []) return state } ``` ### Secure storage for tokens ```tsx import * as SecureStore from 'expo-secure-store' await SecureStore.setItemAsync('auth_token', token) // Keychain / Keystore const token = await SecureStore.getItemAsync('auth_token') ``` ## Code Examples ### A full screen: route → query → list → states ```tsx // app/(tabs)/orders.tsx import { memo, useCallback } from 'react' import { FlatList, Text, View } from 'react-native' import { useQuery } from '@tanstack/react-query' import { z } from 'zod' const OrderSchema = z.object({ id: z.string(), total: z.number(), status: z.string() }) const OrdersSchema = z.array(OrderSchema) type Order = z.infer function useOrders() { return useQuery({ queryKey: ['orders'], queryFn: async () => OrdersSchema.parse(await api.listOrders()), }) } // Memoized so its reference is stable across renders (see the lists guidance). const OrderRow = memo(function OrderRow({ item }: { item: Order }) { return ( #{item.id} {item.status} · ${item.total} ) }) export default function OrdersScreen() { const { data, isLoading, isError, refetch, isRefetching } = useOrders() const renderItem = useCallback(({ item }: { item: Order }) => , []) if (isLoading) return Loading… if (isError) return Could not load orders. if (!data?.length) return No orders yet. return ( o.id} onRefresh={refetch} refreshing={isRefetching} renderItem={renderItem} /> ) } ``` ### A form: React Hook Form + Zod resolver ```tsx import { useForm, Controller } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' import { TextInput, Button, Text } from 'react-native' const Schema = z.object({ email: z.string().email('Invalid email') }) type FormValues = z.infer export function EmailForm({ onSubmit }: { onSubmit: (v: FormValues) => void }) { const { control, handleSubmit, formState: { errors } } = useForm({ resolver: zodResolver(Schema), defaultValues: { email: '' }, }) return ( <> ( )} /> {errors.email && {errors.email.message}}