ryana
LiveView Organization Question: Should I have this function component as an intermediary?
I am building my first Phoenix site. I’ve gone through most of the “Programming Phoenix LiveView” Bruce A. Tate and Sophie Benedetto and read a lot of the LiveView docs, but I’m stumbling a bit on organizing some code. Specifically on the relationship whether I should use a function component or not.
I’m building a site to hold video tutorials for my clients. Users see an archive of videos. They can favorite videos from this screen.
In the first iteration I have a video archive LiveView that contains a video card LiveComponent. After reading “Programming Phoenix LiveView” I thought it might be better to have the video card be a function component, and isolate the favorite icon and favoriting functionality in its own LiveComponent.
LiveView → LiveComponent
or
LiveView → FunctionComponent → LiveComponent
Is one approach considered more correct than the other?
Most Liked
mindok
All of what @DaAnalyst said, but I’ll add one more thing…
Function components work in “dead views” (can’t recall where we all landed on what to call them - I’m talking about traditional view/controller architecture) whereas LiveComponents don’t.
From a dependency flow point of view, I’d personally only really consider invoking a LiveComponent from a function component if that function component was defined in (or very close to) the LiveView module that calls it. Project level function components should be kept clean enough to be used anywhere, including traditional view/controller pages, and should therefore not have LiveComponents in them.
DaAnalyst
It’s hard to tell by your description, but here’s a hint:
Use function components for:
- reusing a (reoccurring) part of the template
- breaking an otherwise large template into smaller organizational parts
- validating (to a certain extent) the attributes and/or defining their default values (in LiveComponents you need to do this one yourself)
- note: they can’t keep or manage any state
Therefore, if you really want to wrap a LiveComponent into a function component, you can.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









