Skip to content

Commit 9d1a9f7

Browse files
committed
Add documentation around deep merging shared data
1 parent f4df2aa commit 9d1a9f7

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,67 @@ class EventsController < ApplicationController
101101
end
102102
```
103103

104+
#### Deep Merging Shared Data
105+
106+
By default, Inertia will shallow merge data defined in an action with the shared data. You might want a deep merge. Imagine using shared data to represent defaults you'll override sometimes.
107+
108+
```ruby
109+
class ApplicationController
110+
inertia_share do
111+
{ basketball_data: { points: 50, rebounds: 100 } }
112+
end
113+
end
114+
```
115+
116+
Let's say we want a particular action to change only part of that data structure. The renderer accepts a `deep_merge` option:
117+
118+
```ruby
119+
class CrazyScorersController < ApplicationController
120+
def index
121+
render inertia: 'CrazyScorersComponent',
122+
props: { basketball_data: { points: 100 } },
123+
deep_merge: true
124+
end
125+
end
126+
127+
# The renderer will send this to the frontend:
128+
{
129+
basketball_data: {
130+
points: 100,
131+
rebounds: 100,
132+
}
133+
}
134+
```
135+
136+
Deep merging can be set as the project wide default via the InertiaRails configuration:
137+
138+
```ruby
139+
# config/initializers/some_initializer.rb
140+
InertiaRails.configure do |config|
141+
config.deep_merge_shared_data = true
142+
end
143+
144+
```
145+
146+
If deep merging is enabled by default, it's possible to opt out within the action:
147+
148+
```ruby
149+
class CrazyScorersController < ApplicationController
150+
def index
151+
render inertia: 'CrazyScorersComponent',
152+
props: { basketball_data: { points: 100 } },
153+
deep_merge: false
154+
end
155+
end
156+
157+
# Even if deep merging is set by default, since the renderer has `deep_merge: false`, it will send a shallow merge to the frontend:
158+
{
159+
basketball_data: {
160+
points: 100,
161+
}
162+
}
163+
```
164+
104165
### Lazy Props
105166

106167
On the front end, Inertia supports the concept of "partial reloads" where only the props requested are returned by the server. Sometimes, you may want to use this flow to avoid processing a particularly slow prop on the intial load. In this case, you can use Lazy props. Lazy props aren't evaluated unless they're specifically requested by name in a partial reload.
@@ -139,6 +200,8 @@ InertiaRails.configure do |config|
139200
# ssr specific options
140201
config.ssr_enabled = false
141202
config.ssr_url = 'http://localhost:13714'
203+
204+
config.deep_merge_shared_data = false
142205

143206
end
144207
```

0 commit comments

Comments
 (0)