SKILL.blade.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ---
  2. name: inertia-react-development
  3. description: "Develops Inertia.js v1 React client-side applications. Activates when creating React pages, forms, or navigation; using Link or router; or when user mentions React with Inertia, React pages, React forms, or React navigation."
  4. license: MIT
  5. metadata:
  6. author: laravel
  7. ---
  8. @php
  9. /** @var \Laravel\Boost\Install\GuidelineAssist $assist */
  10. @endphp
  11. # Inertia React Development
  12. ## Documentation
  13. Use `search-docs` for detailed Inertia v1 React patterns and documentation.
  14. ## Basic Usage
  15. ### Page Components Location
  16. React page components should be placed in the `{{ $assist->inertia()->pagesDirectory() }}` directory.
  17. ### Page Component Structure
  18. @boostsnippet("Basic React Page Component", "react")
  19. export default function UsersIndex({ users }) {
  20. return (
  21. <div>
  22. <h1>Users</h1>
  23. <ul>
  24. {users.map(user => <li key={user.id}>{user.name}</li>)}
  25. </ul>
  26. </div>
  27. )
  28. }
  29. @endboostsnippet
  30. ### Client-Side Navigation
  31. Use `<Link>` for client-side navigation (not `<a>` tags):
  32. @boostsnippet("Inertia React Navigation", "react")
  33. import { Link } from '@inertiajs/react'
  34. <Link href="/">Home</Link>
  35. <Link href="/users">Users</Link>
  36. <Link href={`/users/${user.id}`}>View User</Link>
  37. @endboostsnippet
  38. ### Link With Method
  39. @boostsnippet("Link with POST Method", "react")
  40. import { Link } from '@inertiajs/react'
  41. <Link href="/logout" method="post" as="button">
  42. Logout
  43. </Link>
  44. @endboostsnippet
  45. ### Programmatic Navigation
  46. @boostsnippet("Router Visit", "react")
  47. import { router } from '@inertiajs/react'
  48. function handleClick() {
  49. router.visit('/users')
  50. }
  51. // Or with options
  52. router.visit('/users', {
  53. method: 'post',
  54. data: { name: 'John' },
  55. onSuccess: () => console.log('Success!'),
  56. })
  57. @endboostsnippet
  58. ## Form Handling
  59. ### Using `router.post`
  60. @boostsnippet("Form With router.post", "react")
  61. import { router } from '@inertiajs/react'
  62. import { useState } from 'react'
  63. export default function CreateUser() {
  64. const [values, setValues] = useState({
  65. name: '',
  66. email: '',
  67. })
  68. const [processing, setProcessing] = useState(false)
  69. function handleSubmit(e) {
  70. e.preventDefault()
  71. setProcessing(true)
  72. router.post('/users', values, {
  73. onFinish: () => setProcessing(false),
  74. })
  75. }
  76. return (
  77. <form onSubmit={handleSubmit}>
  78. <input
  79. type="text"
  80. value={values.name}
  81. onChange={e => setValues({ ...values, name: e.target.value })}
  82. />
  83. <input
  84. type="email"
  85. value={values.email}
  86. onChange={e => setValues({ ...values, email: e.target.value })}
  87. />
  88. <button type="submit" disabled={processing}>
  89. Create User
  90. </button>
  91. </form>
  92. )
  93. }
  94. @endboostsnippet
  95. ### Using `useForm` Hook (If Available)
  96. Check the Inertia documentation to confirm if `useForm` is available in your version:
  97. @boostsnippet("useForm Hook Example", "react")
  98. import { useForm } from '@inertiajs/react'
  99. export default function CreateUser() {
  100. const { data, setData, post, processing, errors } = useForm({
  101. name: '',
  102. email: '',
  103. })
  104. function submit(e) {
  105. e.preventDefault()
  106. post('/users')
  107. }
  108. return (
  109. <form onSubmit={submit}>
  110. <input
  111. type="text"
  112. value={data.name}
  113. onChange={e => setData('name', e.target.value)}
  114. />
  115. {errors.name && <div>{errors.name}</div>}
  116. <input
  117. type="email"
  118. value={data.email}
  119. onChange={e => setData('email', e.target.value)}
  120. />
  121. {errors.email && <div>{errors.email}</div>}
  122. <button type="submit" disabled={processing}>
  123. Create User
  124. </button>
  125. </form>
  126. )
  127. }
  128. @endboostsnippet
  129. ## Inertia v1 Limitations
  130. Inertia v1 does not support these v2 features:
  131. - `<Form>` component
  132. - Deferred props
  133. - Prefetching
  134. - Polling
  135. - Infinite scrolling with `WhenVisible`
  136. - Merging props
  137. Do not use these features in v1 projects.
  138. ## Common Pitfalls
  139. - Using traditional `<a>` links instead of Inertia's `<Link>` component (breaks SPA behavior)
  140. - Trying to use Inertia v2 features (deferred props, `<Form>` component, etc.) in v1 projects
  141. - Using `<form>` without preventing default submission (use `e.preventDefault()`)
  142. - Not handling loading states during form submission