--- name: inertia-react-development description: "Develops Inertia.js v2 React client-side applications. Activates when creating React pages, forms, or navigation; using ,
, useForm, or router; working with deferred props, prefetching, or polling; or when user mentions React with Inertia, React pages, React forms, or React navigation." license: MIT metadata: author: laravel --- # Inertia React Development ## When to Apply Activate this skill when: - Creating or modifying React page components for Inertia - Working with forms in React (using `` or `useForm`) - Implementing client-side navigation with `` or `router` - Using v2 features: deferred props, prefetching, WhenVisible, InfiniteScroll, once props, flash data, or polling - Building React-specific features with the Inertia protocol ## Documentation Use `search-docs` for detailed Inertia v2 React patterns and documentation. ## Basic Usage ### Page Components Location React page components should be placed in the `resources/js/pages` directory. ### Page Component Structure ```react export default function UsersIndex({ users }) { return (

Users

) } ``` ## Client-Side Navigation ### Basic Link Component Use `` for client-side navigation instead of traditional `` tags: ```react import { Link, router } from '@inertiajs/react' Home Users View User ``` ### Link with Method ```react import { Link } from '@inertiajs/react' Logout ``` ### Prefetching Prefetch pages to improve perceived performance: ```react import { Link } from '@inertiajs/react' Users ``` ### Programmatic Navigation ```react import { router } from '@inertiajs/react' function handleClick() { router.visit('/users') } // Or with options router.visit('/users', { method: 'post', data: { name: 'John' }, onSuccess: () => console.log('Success!'), }) ``` ## Form Handling ### Form Component (Recommended) The recommended way to build forms is with the `` component: ```react import { Form } from '@inertiajs/react' export default function CreateUser() { return ( {({ errors, processing, wasSuccessful }) => ( <> {errors.name &&
{errors.name}
} {errors.email &&
{errors.email}
} {wasSuccessful &&
User created!
} )} ) } ``` ### Form Component With All Props ```react import { Form } from '@inertiajs/react'
{({ errors, hasErrors, processing, progress, wasSuccessful, recentlySuccessful, clearErrors, resetAndClearErrors, defaults, isDirty, reset, submit }) => ( <> {errors.name &&
{errors.name}
} {progress && ( {progress.percentage}% )} {wasSuccessful &&
Saved!
} )}
``` ### Form Component Reset Props The `
` component supports automatic resetting: - `resetOnError` - Reset form data when the request fails - `resetOnSuccess` - Reset form data when the request succeeds - `setDefaultsOnSuccess` - Update default values on success Use the `search-docs` tool with a query of `form component resetting` for detailed guidance. ```react import { Form } from '@inertiajs/react' {({ errors, processing, wasSuccessful }) => ( <> {errors.name &&
{errors.name}
} )}
``` Forms can also be built using the `useForm` helper for more programmatic control. Use the `search-docs` tool with a query of `useForm helper` for guidance. ### `useForm` Hook For more programmatic control or to follow existing conventions, use the `useForm` hook: ```react import { useForm } from '@inertiajs/react' export default function CreateUser() { const { data, setData, post, processing, errors, reset } = useForm({ name: '', email: '', password: '', }) function submit(e) { e.preventDefault() post('/users', { onSuccess: () => reset('password'), }) } return (
setData('name', e.target.value)} /> {errors.name &&
{errors.name}
} setData('email', e.target.value)} /> {errors.email &&
{errors.email}
} setData('password', e.target.value)} /> {errors.password &&
{errors.password}
}
) } ``` ## Inertia v2 Features ### Deferred Props Use deferred props to load data after initial page render: ```react export default function UsersIndex({ users }) { // users will be undefined initially, then populated return (

Users

{!users ? (
) : (
    {users.map(user => (
  • {user.name}
  • ))}
)}
) } ``` ### Polling Automatically refresh data at intervals: ```react import { router } from '@inertiajs/react' import { useEffect } from 'react' export default function Dashboard({ stats }) { useEffect(() => { const interval = setInterval(() => { router.reload({ only: ['stats'] }) }, 5000) // Poll every 5 seconds return () => clearInterval(interval) }, []) return (

Dashboard

Active Users: {stats.activeUsers}
) } ``` ### WhenVisible Lazy-load a prop when an element scrolls into view. Useful for deferring expensive data that sits below the fold: ```react import { WhenVisible } from '@inertiajs/react' export default function Dashboard({ stats }) { return (

Dashboard

{/* stats prop is loaded only when this section scrolls into view */} Loading stats...
}> {({ fetching }) => (

Total Users: {stats.total_users}

Revenue: {stats.revenue}

{fetching && Refreshing...}
)} ) } ``` ## Server-Side Patterns Server-side patterns (Inertia::render, props, middleware) are covered in inertia-laravel guidelines. ## Common Pitfalls - Using traditional `
` links instead of Inertia's `` component (breaks SPA behavior) - Forgetting to add loading states (skeleton screens) when using deferred props - Not handling the `undefined` state of deferred props before data loads - Using `
` without preventing default submission (use `` component or `e.preventDefault()`) - Forgetting to check if `` component is available in your Inertia version