Skip to content

Commit 638a269

Browse files
authored
Documentation for the matchOn method (#418)
* Documentation for the `matchOn` method * Bring back default behaviour notice
1 parent d538a19 commit 638a269

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

resources/js/Pages/merging-props.jsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { A, Code, H1, H2, P, Strong, TabbedCode } from '@/Components'
1+
import { A, Code, CodeBlock, H1, H2, Notice, P, TabbedCode } from '@/Components'
22
import dedent from 'dedent-js'
33

44
export const meta = {
55
title: 'Merging props',
66
links: [
77
{ url: '#top', name: 'Introduction' },
88
{ url: '#server-side', name: 'Server side' },
9+
{ url: '#client-side', name: 'Client side' },
10+
{ url: '#combining-with-deferred-props', name: 'Deferred props' },
911
{ url: '#resetting-props', name: 'Resetting props' },
1012
],
1113
}
@@ -71,15 +73,34 @@ export default function () {
7173
]}
7274
/>
7375

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>
7498
<P>
7599
On the client side, Inertia detects that this prop should be merged. If the prop returns an array, it will
76100
append the response to the current prop value. If it's an object, it will merge the response with the current
77101
prop value. If you have opted to <Code>deepMerge</Code>, Inertia ensures a deep merge of the entire structure.
78102
</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>
83104
<P>
84105
You can also combine <A href="/deferred-props">deferred props</A> with mergeable props to defer the loading of
85106
the prop and ultimately mark it as mergeable once it's loaded.

0 commit comments

Comments
 (0)