Let’s say you have multiple streams in your socket assigns and you want to render the items of these streams as items in your UI. I was wondering whether then is required to render those streams as separate lists of UI elements.
For example, is the second option also possible?
- Separate lists
def mount(_, _, socket)
socket
|> stream(:posts, posts)
|> stream(:attachments, attachments)
{:ok, socket}
end
def render(assigns) do
~H"""
<div phx-update="stream">
<%= for {dom_id, post} <- @streams.posts do %>
<.post id={dom_id} post={post}/>
<% end %>
</div>
<div phx-update="stream">
<%= for {dom_id, attachment} <- @streams.attachments do %>
<.attachment id={dom_id} attachment={attachment}/>
<% end %>
</div>
"""
end
- Non-separate lists
def mount(_, _, socket)
socket
|> stream(:posts, posts)
|> stream(:attachments, attachments)
{:ok, socket}
end
def render(assigns) do
~H"""
<div phx-update="stream">
<%= for {dom_id, post} <- @streams.posts do %>
<.post id={dom_id} post={post}/>
<%= for {dom_id, attachment} <- @streams.attachments do %>
<.attachment :if={post.attachments_id == attachments.id} id={dom_id} attachment={attachment}/>
<% end %>
<% end %>
</div>
"""
end
I have tried the latter scenario. The initial render is ok. I cannot get the UI to update, however. Just wanted to check if I am overlooking something, or whether it simply isn’t possible.