--- name: react description: This skill should be used when working with React 19, including hooks, components, server components, concurrent features, and React DOM APIs. Provides comprehensive knowledge of React patterns, best practices, and modern React architecture. --- # React 19 Skill This skill provides comprehensive knowledge and patterns for working with React 19 effectively in modern applications. ## When to Use This Skill Use this skill when: - Building React applications with React 19 features - Working with React hooks and component patterns - Implementing server components and server functions - Using concurrent features and transitions - Optimizing React application performance - Troubleshooting React-specific issues - Working with React DOM APIs and client/server rendering - Using React Compiler features ## Core Concepts ### React 19 Overview React 19 introduces significant improvements: - **Server Components** - Components that render on the server - **Server Functions** - Functions that run on the server from client code - **Concurrent Features** - Better performance with concurrent rendering - **React Compiler** - Automatic memoization and optimization - **Form Actions** - Built-in form handling with useActionState - **Improved Hooks** - New hooks like useOptimistic, useActionState - **Better Hydration** - Improved SSR and hydration performance ### Component Fundamentals Use functional components with hooks: ```typescript // Functional component with props interface interface ButtonProps { label: string onClick: () => void variant?: 'primary' | 'secondary' } const Button = ({ label, onClick, variant = 'primary' }: ButtonProps) => { return ( ) } ``` **Key Principles:** - Use functional components over class components - Define prop interfaces in TypeScript - Use destructuring for props - Provide default values for optional props - Keep components focused and composable ## React Hooks Reference ### State Hooks #### useState Manage local component state: ```typescript const [count, setCount] = useState(0) const [user, setUser] = useState(null) // Named return variables pattern const handleIncrement = () => { setCount(prev => prev + 1) // Functional update } // Update object state immutably setUser(prev => prev ? { ...prev, name: 'New Name' } : null) ``` #### useReducer Manage complex state with reducer pattern: ```typescript type State = { count: number; status: 'idle' | 'loading' } type Action = | { type: 'increment' } | { type: 'decrement' } | { type: 'setStatus'; status: State['status'] } const reducer = (state: State, action: Action): State => { switch (action.type) { case 'increment': return { ...state, count: state.count + 1 } case 'decrement': return { ...state, count: state.count - 1 } case 'setStatus': return { ...state, status: action.status } default: return state } } const [state, dispatch] = useReducer(reducer, { count: 0, status: 'idle' }) ``` #### useActionState Handle form actions with pending states (React 19): ```typescript const [state, formAction, isPending] = useActionState( async (previousState: FormState, formData: FormData) => { const name = formData.get('name') as string // Server action or async operation const result = await saveUser({ name }) return { success: true, data: result } }, { success: false, data: null } ) return (
) ``` ### Effect Hooks #### useEffect Run side effects after render: ```typescript // Named return variables preferred useEffect(() => { const controller = new AbortController() const fetchData = async () => { const response = await fetch('/api/data', { signal: controller.signal }) const data = await response.json() setData(data) } fetchData() // Cleanup function return () => { controller.abort() } }, [dependencies]) // Dependencies array ``` **Key Points:** - Always return cleanup function for subscriptions - Use dependency array correctly to avoid infinite loops - Don't forget to handle race conditions with AbortController - Effects run after paint, not during render #### useLayoutEffect Run effects synchronously after DOM mutations but before paint: ```typescript useLayoutEffect(() => { // Measure DOM nodes const height = ref.current?.getBoundingClientRect().height setHeight(height) }, []) ``` Use when you need to: - Measure DOM layout - Synchronously re-render before browser paints - Prevent visual flicker #### useInsertionEffect Insert styles before any DOM reads (for CSS-in-JS libraries): ```typescript useInsertionEffect(() => { const style = document.createElement('style') style.textContent = '.my-class { color: red; }' document.head.appendChild(style) return () => { document.head.removeChild(style) } }, []) ``` ### Performance Hooks #### useMemo Memoize expensive calculations: ```typescript const expensiveValue = useMemo(() => { return computeExpensiveValue(a, b) }, [a, b]) ``` **When to use:** - Expensive calculations that would slow down renders - Creating stable object references for dependency arrays - Optimizing child component re-renders **When NOT to use:** - Simple calculations (overhead not worth it) - Values that change frequently #### useCallback Memoize callback functions: ```typescript const handleClick = useCallback(() => { console.log('Clicked', value) }, [value]) // Pass to child that uses memo ``` **Use when:** - Passing callbacks to optimized child components - Function is a dependency in another hook - Function is used in effect cleanup ### Ref Hooks #### useRef Store mutable values that don't trigger re-renders: ```typescript // DOM reference const inputRef = useRef(null) useEffect(() => { inputRef.current?.focus() }, []) // Mutable value storage const countRef = useRef(0) countRef.current += 1 // Doesn't trigger re-render ``` #### useImperativeHandle Customize ref handle for parent components: ```typescript interface InputHandle { focus: () => void clear: () => void } const CustomInput = forwardRef((props, ref) => { const inputRef = useRef(null) useImperativeHandle(ref, () => ({ focus: () => { inputRef.current?.focus() }, clear: () => { if (inputRef.current) { inputRef.current.value = '' } } })) return }) ``` ### Context Hooks #### useContext Access context values: ```typescript // Create context interface ThemeContext { theme: 'light' | 'dark' toggleTheme: () => void } const ThemeContext = createContext(null) // Provider const ThemeProvider = ({ children }: { children: React.ReactNode }) => { const [theme, setTheme] = useState<'light' | 'dark'>('light') const toggleTheme = useCallback(() => { setTheme(prev => prev === 'light' ? 'dark' : 'light') }, []) return ( {children} ) } // Consumer const ThemedButton = () => { const context = useContext(ThemeContext) if (!context) throw new Error('useTheme must be used within ThemeProvider') const { theme, toggleTheme } = context return ( ) } ``` ### Transition Hooks #### useTransition Mark state updates as non-urgent: ```typescript const [isPending, startTransition] = useTransition() const handleTabChange = (newTab: string) => { startTransition(() => { setTab(newTab) // Non-urgent update }) } return ( <> {isPending && } ) ``` **Use for:** - Marking expensive updates as non-urgent - Keeping UI responsive during state transitions - Preventing loading states for quick updates #### useDeferredValue Defer re-rendering for non-urgent updates: ```typescript const [query, setQuery] = useState('') const deferredQuery = useDeferredValue(query) // Use deferred value for expensive rendering const results = useMemo(() => { return searchResults(deferredQuery) }, [deferredQuery]) return ( <> setQuery(e.target.value)} /> ) ``` ### Optimistic Updates #### useOptimistic Show optimistic state while async operation completes (React 19): ```typescript const [optimisticMessages, addOptimisticMessage] = useOptimistic( messages, (state, newMessage: string) => [ ...state, { id: 'temp', text: newMessage, pending: true } ] ) const handleSend = async (formData: FormData) => { const message = formData.get('message') as string // Show optimistic update immediately addOptimisticMessage(message) // Send to server await sendMessage(message) } return ( <> {optimisticMessages.map(msg => (
{msg.text}
))}
) ``` ### Other Hooks #### useId Generate unique IDs for accessibility: ```typescript const id = useId() return ( <> ) ``` #### useSyncExternalStore Subscribe to external stores: ```typescript const subscribe = (callback: () => void) => { store.subscribe(callback) return () => store.unsubscribe(callback) } const getSnapshot = () => store.getState() const getServerSnapshot = () => store.getInitialState() const state = useSyncExternalStore( subscribe, getSnapshot, getServerSnapshot ) ``` #### useDebugValue Display custom label in React DevTools: ```typescript const useCustomHook = (value: string) => { useDebugValue(value ? `Active: ${value}` : 'Inactive') return value } ``` ## React Components ### Fragment Group elements without extra DOM nodes: ```typescript // Short syntax <> // Full syntax (when you need key prop)
{item.term}
{item.description}
``` ### Suspense Show fallback while loading: ```typescript }> // With error boundary }> }> ``` ### StrictMode Enable additional checks in development: ```typescript ``` **StrictMode checks:** - Warns about deprecated APIs - Detects unexpected side effects - Highlights potential problems - Double-invokes functions to catch bugs ### Profiler Measure rendering performance: ```typescript const onRender = ( id: string, phase: 'mount' | 'update', actualDuration: number, baseDuration: number, startTime: number, commitTime: number ) => { console.log(`${id} took ${actualDuration}ms`) } ``` ## React APIs ### memo Prevent unnecessary re-renders: ```typescript const ExpensiveComponent = memo(({ data }: Props) => { return
{data}
}, (prevProps, nextProps) => { // Return true if props are equal (skip render) return prevProps.data === nextProps.data }) ``` ### lazy Code-split components: ```typescript const Dashboard = lazy(() => import('./Dashboard')) }> ``` ### startTransition Mark updates as transitions imperatively: ```typescript startTransition(() => { setTab('profile') }) ``` ### cache (React Server Components) Cache function results per request: ```typescript const getUser = cache(async (id: string) => { return await db.user.findUnique({ where: { id } }) }) ``` ### use (React 19) Read context or promises in render: ```typescript // Read context const theme = use(ThemeContext) // Read promise (must be wrapped in Suspense) const data = use(fetchDataPromise) ``` ## Server Components & Server Functions ### Server Components Components that run only on the server: ```typescript // app/page.tsx (Server Component by default) const Page = async () => { // Can fetch data directly const posts = await db.post.findMany() return (
{posts.map(post => ( ))}
) } export default Page ``` **Benefits:** - Direct database access - Zero bundle size for server-only code - Automatic code splitting - Better performance ### Server Functions Functions that run on server, callable from client: ```typescript 'use server' export async function createPost(formData: FormData) { const title = formData.get('title') as string const content = formData.get('content') as string const post = await db.post.create({ data: { title, content } }) revalidatePath('/posts') return post } ``` **Usage from client:** ```typescript 'use client' import { createPost } from './actions' const PostForm = () => { const [state, formAction] = useActionState(createPost, null) return (