|
1 |
| -import { A, Code, H1, H2, P, Strong, TabbedCode } from '@/Components' |
| 1 | +import { A, Code, CodeBlock, H1, H2, Notice, P, TabbedCode } from '@/Components' |
2 | 2 | import dedent from 'dedent-js'
|
3 | 3 |
|
4 | 4 | export const meta = {
|
5 | 5 | title: 'Merging props',
|
6 | 6 | links: [
|
7 | 7 | { url: '#top', name: 'Introduction' },
|
8 | 8 | { url: '#server-side', name: 'Server side' },
|
| 9 | + { url: '#client-side', name: 'Client side' }, |
| 10 | + { url: '#combining-with-deferred-props', name: 'Deferred props' }, |
9 | 11 | { url: '#resetting-props', name: 'Resetting props' },
|
10 | 12 | ],
|
11 | 13 | }
|
@@ -71,15 +73,34 @@ export default function () {
|
71 | 73 | ]}
|
72 | 74 | />
|
73 | 75 |
|
| 76 | + <P> |
| 77 | + During the merging process, if the value is an array, the incoming items will be{' '} |
| 78 | + <em>appended</em> to the existing array, not merged by index. However, you may chain the{' '} |
| 79 | + <Code>matchOn</Code> method to determine how existing items should be matched and updated. |
| 80 | + </P> |
| 81 | + <CodeBlock |
| 82 | + language="php" |
| 83 | + children={dedent` |
| 84 | + Inertia::render('Users/Index', [ |
| 85 | + 'users' => Inertia::deepMerge($users)->matchOn('data.id'), |
| 86 | + ]); |
| 87 | + `} |
| 88 | + /> |
| 89 | + <P> |
| 90 | + In this example, Inertia will iterate over the <Code>users.data</Code> array and attempt to{' '} |
| 91 | + match each item by its <Code>id</Code> field. If a match is found, the existing item will be replaced. |
| 92 | + If no match is found, the new item will be appended. |
| 93 | + </P> |
| 94 | + <Notice> |
| 95 | + You may also pass an array of keys to <Code>matchOn</Code> to specify multiple keys for matching. |
| 96 | + </Notice> |
| 97 | + <H2>Client side</H2> |
74 | 98 | <P>
|
75 | 99 | On the client side, Inertia detects that this prop should be merged. If the prop returns an array, it will
|
76 | 100 | append the response to the current prop value. If it's an object, it will merge the response with the current
|
77 | 101 | prop value. If you have opted to <Code>deepMerge</Code>, Inertia ensures a deep merge of the entire structure.
|
78 | 102 | </P>
|
79 |
| - <P> |
80 |
| - <Strong>Of note:</Strong> During the merging process, if the value is an array, the incoming items will be{' '} |
81 |
| - <em>appended</em> to the existing array, not merged by index. |
82 |
| - </P> |
| 103 | + <H2>Combining with deferred props</H2> |
83 | 104 | <P>
|
84 | 105 | You can also combine <A href="/deferred-props">deferred props</A> with mergeable props to defer the loading of
|
85 | 106 | the prop and ultimately mark it as mergeable once it's loaded.
|
|
0 commit comments