kminevskiy

kminevskiy

Hey folks,

Does anyone know what could cause the following behavior: I have a live view template and in it I conditionally render a form (based on the value inside the socket assigns).

<%= if length(@users) > 0 do %>
...
<% end %>

When the length changes, it used to render a form, but now nothing happens. If I prepend that first line with, say, a tag <p><%= length(@users) %></p> Everything (showing the form dynamically based on the length of @users list) works as expected. I am pretty sure the code worked as is a few weeks ago before I updated to the latest Live View (but I’m not even sure it’s an issue with Live View), among other packages. Am I missing some new behavior introduced recently or is there something else I’m not doing correctly?

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If you updated live view, make sure to rm -rf node_modules && npm install again, sometimes it doesn’t pick up changes.

kminevskiy

kminevskiy OP

Unfortunately, that didn’t help.

The following code “fixes” the problem (I could make that paragraph tag hidden). But… I wonder why this is happening at all and what exactly changed after the update. Went through the changelog in LiveView (I was running 0.10.0 prior to updating to 0.12.1), but found nothing related to my issue.

<p><%= length(@users) %></p>
<%= if length(@users) > 0 do %>
...
<% end %>
NobbZ

NobbZ

You shouldn’t to length like this. This will always iterate and count the full list.

Use something like match?([_|_], @users) instead.

kminevskiy

kminevskiy OP

That’s a great tip, thank you!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Enum.any?(@users) is probably more idiomatic. But none of that should affect whether or not the page updates. @kminevskiy be sure to check the rendered page for HTML validity, if the HTML is invalid this can negatively affect morphdom’s ability to handle updates.

Can you provide more code? Maybe IO.inspect(@users) in the template and see if the output is what you expect?

kminevskiy

kminevskiy OP

Thanks for your suggestions.

I remember a few days ago, after I updated the LiveView package, I started the app as usual and was surprised that I couldn’t see the form. Spent some time and through trial and error I found that weird behavior.

IO.inspect(@users) returns a list of maps. Nothing special about it. Plus, I don’t render it in the form - I simply check the length of it and render the form based on the length.

dom

dom

You can put an IO.inspect or something in your form template to make sure it’s called. If it isn’t, one thing to check is that your rendering function is pure, it shouldn’t load users from DB for instance, otherwise Phoenix’s change tracking can’t tell that it changed and won’t re-render it. It still worked on 0.10 but 0.11 is better optimized so it actually matters now.

(BTW, there’s an Enum.empty? function - for lists it checks if list == [] which is as efficient as it gets)

kminevskiy

kminevskiy OP

If I put IO.inspect inside the template, it does properly return a list of users (and it gets updated on events). So the list is populated correctly. I guess I’ll keep digging and trying things.

axelson

axelson

Scenic Core Team

Maybe you could try the master branch. I encountered a fairly similar problem with content disappearing that Chris McCord has fixed on the master branch. I think it is this changelog entry: “Fix component innerHTML being discarded when a sibling DOM element appears above it, in cases where the component lacks a DOM id”

kminevskiy

kminevskiy OP

I was looking at that fix a few minutes ago. I’ll update to 0.12.1 and report back.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
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
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews