anagrius

anagrius

Hey,

I try to keep my application state free of derived state. So I don’t for instance have:

{a: 1, b: 2, sum: 3}

But rather compute the derived state when I need it.
In liveview applications you often need derived state while rendering HTML, and it natural to do the calculation once and store the derived state in assigns so it can be used with the @-notation:

<div><%= @sum %></div>

The alternative is of course:

<div><%= @a + @b %></div>

in this trivial example, but often it is much more complex calculations involving many inputs, and these have to be passed to the calculation function:

<div><%= calculate_derived_state(@a, @b, @settings.displayFormat, @x, @y, @z) %></div>

Again fine if it is used once or twice.

Reassigning assigns in function components is allowed which means I can do:

def some_component(assigns) do
    assigns = assign(assigns, :sum, assigns.a + assigns.b)
    ~H"""
    <div><%= @sum %>
    """
end

But this is not allowed in the top-level render function of a liveview or live-component.

So, my question: Why would I not just do:

def render(assigns) do
    ~H"""
    <.render_inner {assigns} />
    """
end

def render_inner(assigns) do
      assigns = assign(assigns, :sum, assigns.a + assigns.b)
     ~H"""
      <div><%= @sum %></div>
     """
end

I understand that sum is calculated each render, but that is a small cost to avoid managing stale derived state.

Is there some hidden cost I don’t see? Doe passing {assigns} to render_inner for instance for the rendering of the entire view-tree or something.
I understand that render_inner must be recalculated on each render - but that should be equivalent to what render would do anyway.

Showing Posts 1 to 4

sodapopcan

sodapopcan

Citation needed for this one! Maybe this is a new thing as my project is not using 1.0 yet but this is the first I’ve heard of this! I could be wrong.

Putting the raw assigns variable directly in a template means that you won’t get the benefit of change-tracking. This effects what get sent back over the wire, so you will end up with really inefficient diffs.

steffend

steffend

Phoenix Core Team

You’re not wrong. Using the assign function in the render function of a LV or LC is perfectly valid.

anagrius

anagrius OP

Unlike LiveView’s render/1 callback, a function component can modify the assigns it receives via the assign/2 , assign/3 , assign_new/3 , and update/3 functions. Therefore, you can assign the computed values before declaring your template.

Maybe it is just outdated? Since it works perfectly well in render/1. It also says you “can’t” which you can, not that you shouldn’t.

sodapopcan

sodapopcan

Oh right you are. Should probably report that as it’s quite confusing, especially when it does indeed say “can’t” when you clearly can. I’ve been doing this for years at this point, though, lol. Only once in a while, but never thought too much about it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

webofbits
I’ve released BeamConsole 0.1.0, an embeddable, read-only process flight recorder and process map for Phoenix and BEAM applications. Its...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews