Skip to content

Commit 99ed2d2

Browse files
committed
Update responses.jsx
1 parent 8f17637 commit 99ed2d2

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

resources/js/Pages/responses.jsx

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const meta = {
55
title: 'Responses',
66
links: [
77
{ url: '#creating-responses', name: 'Creating responses' },
8-
{ url: '#props', name: 'Props' },
8+
{ url: '#properties', name: 'Properties' },
99
{ url: '#provides-inertia-property', name: 'ProvidesInertiaProperty interface' },
1010
{ url: '#provides-inertia-properties', name: 'ProvidesInertiaProperties interface' },
1111
{ url: '#root-template-data', name: 'Root template data' },
@@ -21,10 +21,10 @@ export default function () {
2121
<P>
2222
Creating an Inertia response is simple. To get started, invoke the <Code>Inertia::render()</Code> method within
2323
your controller or route, providing both the name of the <A href="/pages">JavaScript page component</A> that you
24-
wish to render, as well as any props (data) for the page.
24+
wish to render, as well as any properties (data) for the page.
2525
</P>
2626
<P>
27-
In the example below, we will pass a single prop (<Code>event</Code>) which contains four attributes (
27+
In the example below, we will pass a single property (<Code>event</Code>) which contains four attributes (
2828
<Code>id</Code>, <Code>title</Code>, <Code>start_date</Code> and <Code>description</Code>) to the{' '}
2929
<Code>Event/Show</Code> page component.
3030
</P>
@@ -69,9 +69,9 @@ export default function () {
6969
To ensure that pages load quickly, only return the minimum data required for the page. Also, be aware that all
7070
data returned from the controllers will be visible client-side, so be sure to omit sensitive information.
7171
</Notice>
72-
<H2>Props</H2>
72+
<H2>Properties</H2>
7373
<P>
74-
To pass data from the server to your page components, you can use props. You can pass various types of values as
74+
To pass data from the server to your page components, you can use properties. You can pass various types of values as
7575
props, including primitive types, arrays, objects, and several Laravel-specific types that are automatically resolved:
7676
</P>
7777
<TabbedCode
@@ -116,12 +116,12 @@ export default function () {
116116
<H2>ProvidesInertiaProperty interface</H2>
117117
<P>
118118
When passing props to your components, you may want to create custom classes that can transform themselves into the
119-
appropriate data format. You can do this by implementing the <Code>ProvidesInertiaProperty</Code> interface.
119+
appropriate data format. While Laravel's <Code>Arrayable</Code> interface simply converts objects to arrays, Inertia
120+
offers the more powerful <Code>ProvidesInertiaProperty</Code> interface for context-aware transformations.
120121
</P>
121122
<P>
122-
This interface requires you to implement a <Code>toInertiaProperty</Code> method that returns the transformed value.
123-
The method receives a <Code>PropertyContext</Code> object which provides access to the property key, current props,
124-
and the request instance.
123+
This interface requires a <Code>toInertiaProperty</Code> method that receives a <Code>PropertyContext</Code> object
124+
containing the property key, current props, and request instance.
125125
</P>
126126
<TabbedCode
127127
examples={[
@@ -165,34 +165,67 @@ export default function () {
165165
]}
166166
/>
167167
<P>
168-
The <Code>avatar</Code> prop will contain the full avatar URL.
169-
</P>
170-
<P>
171-
This is particularly useful when you need to create reusable prop transformations or when you need access to the
172-
property key or other props during transformation.
168+
The <Code>PropertyContext</Code> gives you access to the property key, which enables powerful patterns like merging
169+
with shared data:
173170
</P>
171+
<TabbedCode
172+
examples={[
173+
{
174+
name: 'Laravel',
175+
language: 'php',
176+
code: dedent`
177+
use Inertia\\Inertia;
178+
use Inertia\\PropertyContext;
179+
use Inertia\\ProvidesInertiaProperty;
180+
181+
class MergeWithShared implements ProvidesInertiaProperty
182+
{
183+
public function __construct(protected array $items = []) {}
184+
185+
public function toInertiaProperty(PropertyContext $prop): mixed
186+
{
187+
// Access the property key to get shared data
188+
$shared = Inertia::getShared($prop->key, []);
189+
190+
// Merge with the new items
191+
return array_merge($shared, $this->items);
192+
}
193+
}
194+
195+
// Usage
196+
Inertia::share('notifications', ['Welcome back!']);
197+
198+
return Inertia::render('Dashboard', [
199+
'notifications' => new MergeWithShared(['New message received']),
200+
// Result: ['Welcome back!', 'New message received']
201+
]);
202+
`,
203+
},
204+
]}
205+
/>
174206
<H2>ProvidesInertiaProperties interface</H2>
175207
<P>
176208
In some situations you may want to group related props together for reusability across different pages. You can
177209
accomplish this by implementing the <Code>ProvidesInertiaProperties</Code> interface.
178210
</P>
179211
<P>
180-
This interface requires you to implement a <Code>toInertiaProperties</Code> method that returns an array of
181-
key-value pairs. The method receives a <Code>RenderContext</Code> object which contains the request instance
182-
and component name.
212+
This interface requires a <Code>toInertiaProperties</Code> method that returns an array of key-value pairs. The
213+
method receives a <Code>RenderContext</Code> object containing the request and component name.
183214
</P>
184215
<TabbedCode
185216
examples={[
186217
{
187218
name: 'Laravel',
188219
language: 'php',
189220
code: dedent`
221+
use App\\Models\\User;
222+
use Illuminate\\Container\\Attributes\\CurrentUser;
190223
use Inertia\\RenderContext;
191224
use Inertia\\ProvidesInertiaProperties;
192225
193226
class UserPermissions implements ProvidesInertiaProperties
194227
{
195-
public function __construct(protected User $user) {}
228+
public function __construct(#[CurrentUser] protected User $user) {}
196229
197230
public function toInertiaProperties(RenderContext $context): array
198231
{
@@ -276,7 +309,7 @@ export default function () {
276309
]}
277310
/>
278311
<P>
279-
Sometimes you may even want to provide data to the root template that will not be sent to your JavaScript page /
312+
Sometimes you may even want to provide data to the root template that will not be sent to your JavaScript page
280313
component. This can be accomplished by invoking the <Code>withViewData</Code> method.
281314
</P>
282315
<TabbedCode

0 commit comments

Comments
 (0)