@inertiajs/react 3.0.0-beta.1 → 3.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inertiajs/react",
3
- "version": "3.0.0-beta.1",
3
+ "version": "3.0.0-beta.2",
4
4
  "license": "MIT",
5
5
  "description": "The React adapter for Inertia.js",
6
6
  "contributors": [
@@ -58,9 +58,9 @@
58
58
  },
59
59
  "dependencies": {
60
60
  "@types/lodash-es": "^4.17.12",
61
- "laravel-precognition": "2.0.0-beta.2",
61
+ "laravel-precognition": "2.0.0-beta.3",
62
62
  "lodash-es": "^4.17.23",
63
- "@inertiajs/core": "3.0.0-beta.1"
63
+ "@inertiajs/core": "3.0.0-beta.2"
64
64
  },
65
65
  "scripts": {
66
66
  "build": "pnpm clean && ./build.js && tsc",
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: inertia-react-development
3
- 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."
3
+ description: "Develops Inertia.js v3 React client-side applications. Activates when creating React pages, forms, or navigation; using <Link>, <Form>, useForm, useHttp, useLayoutProps, or router; working with deferred props, prefetching, optimistic updates, instant visits, or polling; or when user mentions React with Inertia, React pages, React forms, or React navigation."
4
4
  license: MIT
5
5
  metadata:
6
6
  author: laravel
@@ -15,14 +15,14 @@ metadata:
15
15
  Activate this skill when:
16
16
 
17
17
  - Creating or modifying React page components for Inertia
18
- - Working with forms in React (using `<Form>` or `useForm`)
18
+ - Working with forms in React (using `<Form>`, `useForm`, or `useHttp`)
19
19
  - Implementing client-side navigation with `<Link>` or `router`
20
- - Using v2 features: deferred props, prefetching, WhenVisible, InfiniteScroll, once props, flash data, or polling
20
+ - Using v3 features: deferred props, prefetching, optimistic updates, instant visits, layout props, HTTP requests, WhenVisible, InfiniteScroll, once props, flash data, or polling
21
21
  - Building React-specific features with the Inertia protocol
22
22
 
23
23
  ## Documentation
24
24
 
25
- Use `search-docs` for detailed Inertia v2 React patterns and documentation.
25
+ Use `search-docs` for detailed Inertia v3 React patterns and documentation.
26
26
 
27
27
  ## Basic Usage
28
28
 
@@ -267,7 +267,127 @@ export default function CreateUser() {
267
267
  }
268
268
  @endboostsnippet
269
269
 
270
- ## Inertia v2 Features
270
+ ## Inertia v3 Features
271
+
272
+ ### HTTP Requests
273
+
274
+ Use the `useHttp` hook for standalone HTTP requests that do not trigger Inertia page visits. It provides the same developer experience as `useForm`, but for plain JSON endpoints.
275
+
276
+ @boostsnippet("useHttp Example", "react")
277
+ import { useHttp } from '@inertiajs/react'
278
+
279
+ export default function Search() {
280
+ const { data, setData, get, processing } = useHttp({
281
+ query: '',
282
+ })
283
+
284
+ function search(e) {
285
+ setData('query', e.target.value)
286
+ get('/api/search', {
287
+ onSuccess: (response) => {
288
+ console.log(response)
289
+ },
290
+ })
291
+ }
292
+
293
+ return (
294
+ <>
295
+ <input value={data.query} onChange={search} />
296
+ {processing && <div>Searching...</div>}
297
+ </>
298
+ )
299
+ }
300
+ @endboostsnippet
301
+
302
+ ### Optimistic Updates
303
+
304
+ Apply data changes instantly before the server responds, with automatic rollback on failure:
305
+
306
+ @boostsnippet("Optimistic Update with Router", "react")
307
+ import { router } from '@inertiajs/react'
308
+
309
+ function like(post) {
310
+ router.optimistic((props) => ({
311
+ post: {
312
+ ...props.post,
313
+ likes: props.post.likes + 1,
314
+ },
315
+ })).post(`/posts/${post.id}/like`)
316
+ }
317
+ @endboostsnippet
318
+
319
+ Optimistic updates also work with `useForm` and the `<Form>` component:
320
+
321
+ @boostsnippet("Optimistic Update with Form Component", "react")
322
+ import { Form } from '@inertiajs/react'
323
+
324
+ <Form
325
+ action="/todos"
326
+ method="post"
327
+ optimistic={(props, data) => ({
328
+ todos: [...props.todos, { id: Date.now(), name: data.name, done: false }],
329
+ })}
330
+ >
331
+ <input type="text" name="name" />
332
+ <button type="submit">Add Todo</button>
333
+ </Form>
334
+ @endboostsnippet
335
+
336
+ ### Instant Visits
337
+
338
+ Navigate to a new page immediately without waiting for the server response. The target component renders right away with shared props, while page-specific props load in the background.
339
+
340
+ @verbatim
341
+ @boostsnippet("Instant Visit with Link", "react")
342
+ import { Link } from '@inertiajs/react'
343
+
344
+ <Link href="/dashboard" component="Dashboard">Dashboard</Link>
345
+
346
+ <Link
347
+ href="/posts/1"
348
+ component="Posts/Show"
349
+ pageProps={{ post: { id: 1, title: 'My Post' } }}
350
+ >
351
+ View Post
352
+ </Link>
353
+ @endboostsnippet
354
+ @endverbatim
355
+
356
+ ### Layout Props
357
+
358
+ Share dynamic data between pages and persistent layouts:
359
+
360
+ @boostsnippet("Layout Props in Layout", "react")
361
+ import { useLayoutProps } from '@inertiajs/react'
362
+
363
+ export default function Layout({ children }) {
364
+ const { title, showSidebar } = useLayoutProps({
365
+ title: 'My App',
366
+ showSidebar: true,
367
+ })
368
+
369
+ return (
370
+ <>
371
+ <header>{title}</header>
372
+ {showSidebar && <aside>Sidebar</aside>}
373
+ <main>{children}</main>
374
+ </>
375
+ )
376
+ }
377
+ @endboostsnippet
378
+
379
+ @boostsnippet("Setting Layout Props from Page", "react")
380
+ import { setLayoutProps } from '@inertiajs/react'
381
+
382
+ export default function Dashboard() {
383
+ setLayoutProps({
384
+ title: 'Dashboard',
385
+ showSidebar: false,
386
+ })
387
+
388
+ return <h1>Dashboard</h1>
389
+ }
390
+ @endboostsnippet
271
391
 
272
392
  ### Deferred Props
273
393
 
@@ -275,7 +395,6 @@ Use deferred props to load data after initial page render:
275
395
 
276
396
  @boostsnippet("Deferred Props with Empty State", "react")
277
397
  export default function UsersIndex({ users }) {
278
- // users will be undefined initially, then populated
279
398
  return (
280
399
  <div>
281
400
  <h1>Users</h1>
@@ -308,7 +427,7 @@ export default function Dashboard({ stats }) {
308
427
  useEffect(() => {
309
428
  const interval = setInterval(() => {
310
429
  router.reload({ only: ['stats'] })
311
- }, 5000) // Poll every 5 seconds
430
+ }, 5000)
312
431
 
313
432
  return () => clearInterval(interval)
314
433
  }, [])
@@ -334,7 +453,6 @@ export default function Dashboard({ stats }) {
334
453
  <div>
335
454
  <h1>Dashboard</h1>
336
455
 
337
- {/* stats prop is loaded only when this section scrolls into view */}
338
456
  <WhenVisible data="stats" buffer={200} fallback={<div className="animate-pulse">Loading stats...</div>}>
339
457
  {({ fetching }) => (
340
458
  <div>
@@ -360,3 +478,5 @@ Server-side patterns (Inertia::render, props, middleware) are covered in inertia
360
478
  - Not handling the `undefined` state of deferred props before data loads
361
479
  - Using `<form>` without preventing default submission (use `<Form>` component or `e.preventDefault()`)
362
480
  - Forgetting to check if `<Form>` component is available in your Inertia version
481
+ - Using `router.cancel()` instead of `router.cancelAll()` (v3 breaking change)
482
+ - Using `router.on('invalid', ...)` or `router.on('exception', ...)` instead of the renamed `httpException` and `networkError` events