--- 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. --- # 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, 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 export default function UsersIndex({ users }) { return (

Users

    {users.map(user =>
  • {user.name}
  • )}
) }
## Client-Side Navigation ### Basic Link Component Use `` for client-side navigation instead of traditional `` tags: import { Link, router } from '@inertiajs/react' Home Users View User ### Link with Method import { Link } from '@inertiajs/react' Logout ### Prefetching Prefetch pages to improve perceived performance: import { Link } from '@inertiajs/react' Users ### Programmatic Navigation 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: 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 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. 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: 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: 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: 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 (Infinite Scroll) Load more data when user scrolls to a specific element: import { WhenVisible } from '@inertiajs/react' export default function UsersList({ users }) { return (
{users.data.map(user => (
{user.name}
))} {users.next_page_url && ( Loading more...
} /> )} ) }
## 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