Athunnea

Athunnea

LiveView functional components code style: slots or attributes?

I have been playing with Functional Components from LiveView 0.17. In some cases you can achieve the same result using either slots or attributes. What do you think works better in the following cases?

Using attributes

<Components.badge text={@text} />
<Components.description_list elements={
  [
    {gettext("Legal Name"), @company.legal_name },
    {gettext("Country"),    @company.country    },
    {gettext("Address"),    @company.address    },
  ]
} />

Using slots

<Components.badge><%= @text %></Components.badge>
<Components.attributes>
  <:attribute label={gettext("Legal Name")}><%= @company.legal_name %></:attribute>
  <:attribute label={gettext("Country")}>   <%= @company.country    %></:attribute>
  <:attribute label={gettext("Address")}>   <%= @company.address    %></:attribute>
</Components.attributes>

This is for text values only. For html blocks I’d use slots.

It seems that using attributes is a bit cleaner, less visual noise. Maybe that is because I personally hate <%= ... %> ever since I started using Rails ages ago. It’s noisy and takes some effort to type. {...} is so much cleaner. On the other hand, using attributes for content feels a bit wrong.

So, attributes or slots?

  • Attributes obviously, it’s cleaner
  • Keep attributes for attributes, use slots for content
0 voters

First Post!

sodapopcan

sodapopcan

I would say instead of either of them, provide a Components.description_item component. If you are building a library and want to give people options, I would make both the elements attribute and description_item available. Either way, I wouldn’t use slots for this.

Where Next?

Popular in Discussions Top

hazardfn
I suppose this question is effectively hackney vs. ibrowse but we are at a point in our project where we have to make a choice between th...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New

We're in Beta

About us Mission Statement