`live_render` creates empty div if rendering is conditional

I display a live_render in one of render’s clauses, according to some_key's value:

def render(assigns = %{some_key: true}) do
  ~L"""
  <%= render_foo(assigns) %>
  """
end

def render(assigns) do
  ~L"""
  <!-- some code -->
  <%= live_render(@socket, MyAppWeb.Live.SomeView, id: "some-id", session: some_data) %>
  <!-- more code -->
  """
end

At first some_key equals false and the live_render displays. Then some_key equals true, and it is no longer displayed as expected. However, when some_key equals false again, the live_render now displays an empty div.

What could be the cause of this?

Hi,

The render/1 function is only called once after the mount/3 function. From that point only changes to assigns will trigger changes to teh dynamic parts of the page ( LiveView Assigns and Leex).

So I don;t think conditional rendering will work because it is tracking the html rendered when the socket connection is established. Are you trying to conditionally show a link or automatically redirect? If you describe what you want to do I am sure we can do it using assigns and your handle functions.

Andrew

Thank you Andrew for the reply!

  • I tried to reproduce the problem in a new live project with same version 13.x. But couldn’t.

  • I replaced live_render by live_component and it then works.

  • Company is using an older version 13.x and there have been fixes on that matter according to Jose, so I think it’s not worth for me to further investigate as an upgrade may simply fix it, and it works now with live_component.

Thanks for helping!