saleyn

saleyn

Using LiveView component inside a Phoenix template

How can a liveview component be rendered inside a Phoenix EEX template?

I have a liveview component

defmodule MyApp.Shared.Status do
  use Surface.LiveComponent

  alias MyApp.Shared.FlashMessage

  data online, :boolean, default: false

  def render(assigns) do
    ~F"""
    <div id={@id} class="status">
      <FlashMessage id="status-flash" />
    </div>
    """
  end

that I would like to render inside the navigation bar “_navbar.html.eex” that is included in the “root.html.eex”:

$ cat root.html.eex
<html lang="en">
  <head>
    <%= csrf_meta_tag() %>
  </head>
  <body>
    <%= render("_navbar.html", assigns) %>
    <%= @inner_content %>
  </body>
</html>

$ cat _navbar.html.eex
<header>
  <nav class="nav-bar">
      <div>
      <!--
        <%= live_component MyApp.Shared.Status, id: :main_status %>
        <MyApp.Shared.Status id="main_status"/>
      -->
      </div>
  </nav>  
</header>

In this last file, “_navbar.html.eex” I tried rendering the Shared.Status component by using either <%= live_component MyApp.Shared.Status, id: :main_status %> or <MyApp.Shared.Status id="main_status"/> to no success. What am I missing?

First Post!

APB9785

APB9785

Creator of ECSx

From the docs:

Components are defined by using Phoenix.LiveComponent and are used by calling Phoenix.LiveView.Helpers.live_component/3 in a parent LiveView. Components run inside the LiveView process but have their own life-cycle.

It seems like you’re trying to call the LiveComponent outside of a LiveView, which doesn’t work because the LC doesn’t spawn up its own process, but instead requires a parent LV process to run inside.

For this case, perhaps a function component would be an acceptable alternative?

Where Next?

Popular in Questions Top

PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement