Handling Phoenix component assigns on a wrapper component?

Hello all! :slight_smile:

First time writing in the forums.

Imagine you have a component A that accepts some x and y assigns. Those are processed inside A to create a new assign z that should be given to component B where x and y aren’t needed. What is the better or optimal approach to reassign the assigns inside A?

I was thinking that I could drop X, Y and assign only Z to an empty assigns %{__changed__: nil} but I don’t know if this is a good practice or not. I’m afraid something can break in the future by using an internal implementation like __changed__.

attr x, :any, required: true
attr y, :any, required: true

def component_A(%{x: x, y: y} = assigns) do
    assigns = assign(%{__changed__: nil,  z: build_z(x, y)}

    ~H"""
    <.component_B z={z} />
    """
end

attr z, :any, required: true

def component_B(assigns) do
    ~H"""
    <% Do something with @z %>
    """
end

You’d just pass only z to B. No need to drop anything.

attr x, :any, required: true
attr y, :any, required: true

def component_A(%{x: x, y: y} = assigns) do
    assigns = assign(assigns, :z, build_z(x, y))

    ~H"""
    <.component_B z={@z} />
    """
end

attr z, :any, required: true

def component_B(assigns) do
    ~H"""
    <% Do something with @z %>
    """
end
1 Like

Isn’t it too much data to give to sigil_H when it only needs some of it?

Maybe it doesn’t matter performance wise… just thinking.

It matters much more to not manually fiddle with change tracking than what you’d save in term of performance – which is also likely close to none.

1 Like

thank you @LostKobrakai for your to-the-point and clear answers :slight_smile: