Skip to content

Commit

Permalink
Add required option to attr/3 in Components guide (phoenixframework#5617
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sheerlox authored Nov 9, 2023
1 parent aeb9266 commit 627d452
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions guides/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule HelloWeb.HelloHTML do

embed_templates "hello_html/*"

attr :messenger, :string
attr :messenger, :string, required: true

def greet(assigns) do
~H"""
Expand All @@ -52,7 +52,7 @@ defmodule HelloWeb.HelloHTML do
end
```

We declared the attributes we accept via `attr` provided by `Phoenix.Component`, then we defined our `greet/1` function which returns the HEEx template.
We declared the attributes we accept via the `attr/3` macro provided by `Phoenix.Component`, then we defined our `greet/1` function which returns the HEEx template.

Next we need to update `show.html.heex`:

Expand All @@ -68,7 +68,7 @@ Since templates are embedded inside the `HelloHTML` module, we were able to invo

If the component was defined elsewhere, we can also type `<HelloWeb.HelloHTML.greet messenger="..." />`.

By declaring attributes, Phoenix will warn if we call the `<.greet />` component without passing attributes. If an attribute is optional, you can specify the `:default` option with a value:
By declaring attributes as required, Phoenix will warn at compile time if we call the `<.greet />` component without passing attributes. If an attribute is optional, you can specify the `:default` option with a value:

```
attr :messenger, :string, default: nil
Expand Down

0 comments on commit 627d452

Please sign in to comment.