DaAnalyst
Can function components be made to work properly with `defdelegate` somehow?
Maybe I’m asking too much and maybe this is a language issue, not a LiveView one but still, can it be made for function components to be defdelegated to from another module and work as good when used from that other module?
By work as good I mean those slot's and attr's reporting compile time errors when something that’s required is not specified.
Ex:
defmodule SpecificComponents do
use Phoenix.Component
attr :id, :string, required: true
attr :name, :string, required: true
attr :role, :string, default: nil
def account( assign)
embed_templates "specific_components/*" # containing `account.html.heex`
end
defmodule OverallComponents do
defdelegate account( assigns), to: SpecificComponents
end
defmodule Example do
...
import OverallCompoenents
def render( assigns) do
~H"""
<div>
<.account id="account" />
</div>
"""
end
end
This compiles (on latest Phoenix, LiveView, Elixir) without complaining the otherwise required name attribute is missing.
Most Liked
LostKobrakai
defdelegate is just a macro replace typing out the following
@doc delegate_to: {SpecificComponents, :account, 1}
def account(assigns) do
SpecificComponents.account(assigns)
end
Nothing here creates’s attrs or slots for OverallComponents.account/1. I’m not sure if the live view compiler could make use of the doc metadata.
Personally I’d argue that this seems like useless complexity. Why not use the SpecificComponents one directly. No indirection and no need to search around where the actual code is. The docs the LSP shows will be correct and so on.
sodapopcan
Well this explains a lot… d’oh me. cc: @dviramontes
But ya, I used a very similar pattern at my last job, mostly so all components could be mass imported.
Popular in Proposals: Ideas
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









