| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- ---
- name: inertia-react-development
- description: "Develops Inertia.js v2 React client-side applications. Activates when creating React pages, forms, or navigation; using <Link>, <Form>, 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
- ---
- @php
- /** @var \Laravel\Boost\Install\GuidelineAssist $assist */
- @endphp
- # Inertia React Development
- ## 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 `{{ $assist->inertia()->pagesDirectory() }}` directory.
- ### Page Component Structure
- @boostsnippet("Basic React Page Component", "react")
- export default function UsersIndex({ users }) {
- return (
- <div>
- <h1>Users</h1>
- <ul>
- {users.map(user => <li key={user.id}>{user.name}</li>)}
- </ul>
- </div>
- )
- }
- @endboostsnippet
- ## Client-Side Navigation
- ### Basic Link Component
- Use `<Link>` for client-side navigation instead of traditional `<a>` tags:
- @boostsnippet("Inertia React Navigation", "react")
- import { Link, router } from '@inertiajs/react'
- <Link href="/">Home</Link>
- <Link href="/users">Users</Link>
- <Link href={`/users/${user.id}`}>View User</Link>
- @endboostsnippet
- ### Link with Method
- @boostsnippet("Link with POST Method", "react")
- import { Link } from '@inertiajs/react'
- <Link href="/logout" method="post" as="button">
- Logout
- </Link>
- @endboostsnippet
- ### Prefetching
- Prefetch pages to improve perceived performance:
- @boostsnippet("Prefetch on Hover", "react")
- import { Link } from '@inertiajs/react'
- <Link href="/users" prefetch>
- Users
- </Link>
- @endboostsnippet
- ### Programmatic Navigation
- @boostsnippet("Router Visit", "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!'),
- })
- @endboostsnippet
- ## Form Handling
- @if($assist->inertia()->hasFormComponent())
- ### Form Component (Recommended)
- The recommended way to build forms is with the `<Form>` component:
- @boostsnippet("Form Component Example", "react")
- import { Form } from '@inertiajs/react'
- export default function CreateUser() {
- return (
- <Form action="/users" method="post">
- {({ errors, processing, wasSuccessful }) => (
- <>
- <input type="text" name="name" />
- {errors.name && <div>{errors.name}</div>}
- <input type="email" name="email" />
- {errors.email && <div>{errors.email}</div>}
- <button type="submit" disabled={processing}>
- {processing ? 'Creating...' : 'Create User'}
- </button>
- {wasSuccessful && <div>User created!</div>}
- </>
- )}
- </Form>
- )
- }
- @endboostsnippet
- ### Form Component With All Props
- @boostsnippet("Form Component Full Example", "react")
- import { Form } from '@inertiajs/react'
- <Form action="/users" method="post">
- {({
- errors,
- hasErrors,
- processing,
- progress,
- wasSuccessful,
- recentlySuccessful,
- clearErrors,
- resetAndClearErrors,
- defaults,
- isDirty,
- reset,
- submit
- }) => (
- <>
- <input type="text" name="name" defaultValue={defaults.name} />
- {errors.name && <div>{errors.name}</div>}
- <button type="submit" disabled={processing}>
- {processing ? 'Saving...' : 'Save'}
- </button>
- {progress && (
- <progress value={progress.percentage} max="100">
- {progress.percentage}%
- </progress>
- )}
- {wasSuccessful && <div>Saved!</div>}
- </>
- )}
- </Form>
- @endboostsnippet
- @if($assist->inertia()->hasFormComponentResets())
- ### Form Component Reset Props
- The `<Form>` 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.
- @boostsnippet("Form with Reset Props", "react")
- import { Form } from '@inertiajs/react'
- <Form
- action="/users"
- method="post"
- resetOnSuccess
- setDefaultsOnSuccess
- >
- {({ errors, processing, wasSuccessful }) => (
- <>
- <input type="text" name="name" />
- {errors.name && <div>{errors.name}</div>}
- <button type="submit" disabled={processing}>
- Submit
- </button>
- </>
- )}
- </Form>
- @endboostsnippet
- @else
- Note: This version of Inertia does not support `resetOnError`, `resetOnSuccess`, or `setDefaultsOnSuccess` on the `<Form>` component. Using these props will cause errors. Upgrade to Inertia v2.2.0+ to use these features.
- @endif
- 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.
- @endif
- ### `useForm` Hook
- @if($assist->inertia()->hasFormComponent() === false)
- For Inertia v2.0.x: Build forms using the `useForm` helper as the `<Form>` component is not available until v2.1.0+.
- @else
- For more programmatic control or to follow existing conventions, use the `useForm` hook:
- @endif
- @boostsnippet("useForm Hook Example", "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 (
- <form onSubmit={submit}>
- <input
- type="text"
- value={data.name}
- onChange={e => setData('name', e.target.value)}
- />
- {errors.name && <div>{errors.name}</div>}
- <input
- type="email"
- value={data.email}
- onChange={e => setData('email', e.target.value)}
- />
- {errors.email && <div>{errors.email}</div>}
- <input
- type="password"
- value={data.password}
- onChange={e => setData('password', e.target.value)}
- />
- {errors.password && <div>{errors.password}</div>}
- <button type="submit" disabled={processing}>
- Create User
- </button>
- </form>
- )
- }
- @endboostsnippet
- ## Inertia v2 Features
- ### Deferred Props
- Use deferred props to load data after initial page render:
- @boostsnippet("Deferred Props with Empty State", "react")
- export default function UsersIndex({ users }) {
- // users will be undefined initially, then populated
- return (
- <div>
- <h1>Users</h1>
- {!users ? (
- <div className="animate-pulse">
- <div className="h-4 bg-gray-200 rounded w-3/4 mb-2"></div>
- <div className="h-4 bg-gray-200 rounded w-1/2"></div>
- </div>
- ) : (
- <ul>
- {users.map(user => (
- <li key={user.id}>{user.name}</li>
- ))}
- </ul>
- )}
- </div>
- )
- }
- @endboostsnippet
- ### Polling
- Use the `usePoll` hook to automatically refresh data at intervals. It handles cleanup on unmount and throttles polling when the tab is inactive.
- @boostsnippet("Basic Polling", "react")
- import { usePoll } from '@inertiajs/react'
- export default function Dashboard({ stats }) {
- usePoll(5000)
- return (
- <div>
- <h1>Dashboard</h1>
- <div>Active Users: {stats.activeUsers}</div>
- </div>
- )
- }
- @endboostsnippet
- @boostsnippet("Polling With Request Options and Manual Control", "react")
- import { usePoll } from '@inertiajs/react'
- export default function Dashboard({ stats }) {
- const { start, stop } = usePoll(5000, {
- only: ['stats'],
- onStart() {
- console.log('Polling request started')
- },
- onFinish() {
- console.log('Polling request finished')
- },
- }, {
- autoStart: false,
- keepAlive: true,
- })
- return (
- <div>
- <h1>Dashboard</h1>
- <div>Active Users: {stats.activeUsers}</div>
- <button onClick={start}>Start Polling</button>
- <button onClick={stop}>Stop Polling</button>
- </div>
- )
- }
- @endboostsnippet
- - `autoStart` (default `true`) — set to `false` to start polling manually via the returned `start()` function
- - `keepAlive` (default `false`) — set to `true` to prevent throttling when the browser tab is inactive
- ### WhenVisible (Infinite Scroll)
- Load more data when user scrolls to a specific element:
- @boostsnippet("Infinite Scroll with WhenVisible", "react")
- import { WhenVisible } from '@inertiajs/react'
- export default function UsersList({ users }) {
- return (
- <div>
- {users.data.map(user => (
- <div key={user.id}>{user.name}</div>
- ))}
- {users.next_page_url && (
- <WhenVisible
- data="users"
- params={{ page: users.current_page + 1 }}
- fallback={<div>Loading more...</div>}
- />
- )}
- </div>
- )
- }
- @endboostsnippet
- ## Common Pitfalls
- - Using traditional `<a>` links instead of Inertia's `<Link>` 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 `<form>` without preventing default submission (use `<Form>` component or `e.preventDefault()`)
- - Forgetting to check if `<Form>` component is available in your Inertia version
|