You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add doc examples using attach_hook for code organization instead of LiveComponents (#3685)
* Update attach_hook/4 with extract handle_event example
* Add small header for doc linking
* Clarify the code-organization sections of the Welcome guide
Add small hierarchy headers to the "Compartmentalize state,
markup, and events" section
Use the guidance in "Functional components or live components?"
at the top of `Phoenix.LiveComponent` to avoid using LiveComponents
only for organization
Provide longer example with a few different alternatives of
what *can* be used for organizing events
* Remove suggested option to delegate events manually
As pointed out, sticking with attach_hook keeps ownership of
the event in the new component
* Remove Helpers and use more-standard {:ok, socket} style
* Add forgotten line break
* Remove 2nd attach_hook example and link directly to the first
* Update lib/phoenix_live_view.ex
* Update guides/introduction/welcome.md
* Update guides/introduction/welcome.md
* Update guides/introduction/welcome.md
* Update guides/introduction/welcome.md
* Update guides/introduction/welcome.md
* Update guides/introduction/welcome.md
* Update guides/introduction/welcome.md
* Reverse locations of long-code-example & link-to-that-example
Now the "using attach_hook/4 to extract handle_events" example
code is actually kept in the doc FOR attach_hook
This keeps the welcome.md page similar to it's original size
* Rename "Organizing code" section -> "Sharing event handling logic"
* Continue minimizing changes to welcome.md
- Merge the "attach_hook/4 to organize event handling" section into
the "Function components" one above it
- Return to the previous bullet-point based summary, adding only a single
line to emphasize that function components can work alone for markup, but
also organize event handling when paired with attach_hook/4
* Apply suggestions from code review
* Apply suggestions from code review
---------
Co-authored-by: Gary Rennie <gazler@gmail.com>
Co-authored-by: Steffen Deusch <steffen@deusch.me>
Co-authored-by: José Valim <jose.valim@gmail.com>
@@ -261,6 +270,11 @@ are more complex than function components themselves. Given they all run in the
261
270
same process, errors in components cause the whole view to fail to render.
262
271
For a complete rundown, see `Phoenix.LiveComponent`.
263
272
273
+
When in doubt over [Functional components or live components?](`Phoenix.LiveComponent#functional-components-or-live-components`), default to the former.
274
+
Rely on the latter only when you need the additional state.
275
+
276
+
### live_render/3 to encapsulate state (with error isolation)
277
+
264
278
Finally, if you want complete isolation between parts of a LiveView, you can
265
279
always render a LiveView inside another LiveView by calling
266
280
[`live_render/3`](`Phoenix.Component.live_render/3`). This child LiveView
@@ -277,9 +291,8 @@ Given that it runs in its own process, a nested LiveView is an excellent tool
277
291
for creating completely isolated UI elements, but it is a slightly expensive
278
292
abstraction if all you want is to compartmentalize markup or events (or both).
279
293
280
-
To sum it up:
281
-
282
-
* use `Phoenix.Component` for code organization and reusing markup
294
+
### Summary
295
+
* use `Phoenix.Component` for code organization and reusing markup (optionally with [`attach_hook/4`](`Phoenix.LiveView.attach_hook/4#sharing-event-handling-logic`) for event handling reuse)
283
296
* use `Phoenix.LiveComponent` for sharing state, markup, and events between LiveViews
284
297
* use nested `Phoenix.LiveView` to compartmentalize state, markup, and events (with error isolation)
0 commit comments