Phoenix component attr definition
The new Phoenix.Component
attr/3 function is awesome. It does compile time validations to all the places that are calling the component and also it comes with nice and useful options. For instance the default
option:
attr :name, :string, default: "there"
def greet(assigns) do
~H"""
<h1>Hey <%= @name %>!</h1>
"""
end
That's very useful. That would totally replace most usages of assign_new
like I used to do:
def greet(assigns) do
assigns = assign_new(assigns, :name, fn -> "there" end)
~H"""
<h1>Hey <%= @name %>!</h1>
"""
end
This way we can call:
<.greet />
And have this html generated:
<h1>Hey there!</h1>
Tweet