How to access a components assigns in the module where you define it but outside the component?

Hi!
I would like to access the assigns that may be passed to components when using it in a LiveView, in the module where I define the component but outside the component function. For example when I use this component in a Liveview passing:

<.Components.heading title="Hello!">
<.Components.greet name="John">
defmodule Components do
  # In Phoenix apps, the line is typically: use MyAppWeb, :html
  use Phoenix.Component

  attr :title, :string, required: true

  def heading(assigns) do
    ~H"""
    <h1><%= @title %></h1>
    """
  end

  attr :name, :string, required: true

  def greet(assigns) do
    ~H"""
    <p>Hello <%= @name %></p>
    """
  end

heading_assigns = *Function that access them;*
# %{title: "Hello!"}
greet_assigns = *Function that access them;*
# %{name: "John"}
end

Thank you.

If I understand correctly, you want to pass assigns to a function and then access them later? If so, this is not possible. Function components are pure (as in side-effect free) functions that do not hold state. They merely render the template and return it.

1 Like

Also the module body is executed at compile time and essentially no longer exists at runtime. But the assigns (especially the values) are not known compile time, but only at runtime.

2 Likes

Thank you, correct. Now I know for sure :slight_smile:

Thank you, yes, I was thinking if I could do it at compile time but it’s clear that it’s not :slight_smile: