Skip to content

Commit 7d6e092

Browse files
committed
Add docs on deep merge
1 parent 457e637 commit 7d6e092

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

docs/guide/merging-props.md

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,37 @@ By default, Inertia overwrites props with the same name when reloading a page. H
44

55
## Server side
66

7-
To specify that a prop should be merged, you can use the `merge` method on the prop value.
7+
> `deep_merge` requires `@inertiajs/core` v2.0.8 or higher, and `inertia_rails` v3.8.0 or higher.
8+
9+
To specify that a prop should be merged, use the `merge` or `deep_merge` method on the prop's value.
10+
11+
Use `merge` for merging simple arrays, and `deep_merge` for handling nested objects that include arrays or complex structures, such as pagination objects.
812

913
```ruby
1014
class UsersController < ApplicationController
1115
include Pagy::Backend
1216

1317
def index
14-
_pagy, records = pagy(User.all)
15-
16-
render inertia: 'Users/Index', props: {
17-
results: InertiaRails.merge { records },
18+
pagy, records = pagy(User.all)
19+
20+
render inertia: {
21+
# simple array:
22+
users: InertiaRails.merge { records.as_json(...) },
23+
# pagination object:
24+
data: InertiaRails.deep_merge {
25+
{
26+
records: records.as_json(...),
27+
pagy: pagy_metadata(pagy)
28+
}
29+
}
1830
}
1931
end
2032
end
2133
```
2234

23-
On the client side, Inertia detects that this prop should be merged. If the prop returns an array, it will append the response to the current prop value. If it's an object, it will merge the response with the current prop value.
35+
On the client side, Inertia detects that this prop should be merged. If the prop returns an array, it will append the response to the current prop value. If it's an object, it will merge the response with the current prop value. If you have opted to `deepMerge`, Inertia ensures a deep merge of the entire structure.
36+
37+
**Of note:** During the merging process, if the value is an array, the incoming items will be _appended_ to the existing array, not merged by index.
2438

2539
You can also combine [deferred props](/guide/deferred-props) with mergeable props to defer the loading of the prop and ultimately mark it as mergeable once it's loaded.
2640

@@ -29,8 +43,18 @@ class UsersController < ApplicationController
2943
include Pagy::Backend
3044

3145
def index
32-
render inertia: 'Users/Index', props: {
33-
results: InertiaRails.defer(merge: true) { pagy(User.all)[1] },
46+
pagy, records = pagy(User.all)
47+
48+
render inertia: {
49+
# simple array:
50+
users: InertiaRails.defer(merge: true) { records.as_json(...) },
51+
# pagination object:
52+
data: InertiaRails.defer(deep_merge: true) {
53+
{
54+
records: records.as_json(...),
55+
pagy: pagy_metadata(pagy)
56+
}
57+
}
3458
}
3559
end
3660
end
@@ -48,23 +72,23 @@ The `reset` request option accepts an array of the props keys you would like to
4872
```js
4973
import { router } from '@inertiajs/vue3'
5074

51-
router.reload({ reset: ['results'] })
75+
router.reload({ reset: ['users'] })
5276
```
5377

5478
== React
5579

5680
```js
5781
import { router } from '@inertiajs/react'
5882

59-
router.reload({ reset: ['results'] })
83+
router.reload({ reset: ['users'] })
6084
```
6185

6286
== Svelte 4|Svelte 5
6387

6488
```js
6589
import { router } from '@inertiajs/svelte'
6690

67-
router.reload({ reset: ['results'] })
91+
router.reload({ reset: ['users'] })
6892
```
6993

7094
:::

0 commit comments

Comments
 (0)