Softknobs

Softknobs

Livecomponent: assign @value not available in eex template. Why such behaviour?

I have created a liveview with a child livecomponent (dialog). I was expecting to be able to define assigns in the child mount function but I get the error:

assign @value not available in eex template.
Please make sure all proper assigns have been set. If this
is a child template, ensure assigns are given explicitly by
the parent template as they are not automatically forwarded.

I was expecting this to work since I assign :value is in the mount function of the child component. Though, the error message suggests that the value has to be “forwarded” by the parent liveview in this case. If this is the case, I don’t understand what are the benefits of livecomponents since part of the logic still has to remain in the parent liveview with values passed to the child component when needed.

On the other hand, using two liveviews for this use case does not seem optimal as it would require the usage of PubSub. The latter seems to be more suited for broadcasting which is not something useful for simple component interactions (eg. show/hide).

Livecomponents examples I found just show simple use of them where one or two values are passed to the component but nothing with more complex livecomponents.

Thanks.

Marked As Solved

mindok

mindok

What have you tried to come to that conclusion. The docs (https://hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html) discuss stateful components managing their own state.

You need to pass in an id in the assigns for a component to be stateful. I have only done a simple test - adding a button to a component that increments a counter when it is clicked. But I can have multiple instances of the same component on the same liveview tracking their own counter - I assume that’s what you mean by declaring their own internal values.

Also Liked

Softknobs

Softknobs

You are right and I did some more research and found an example where the live_component declares its own values:

So it is possible and my conclusion was mostly based on the fact that there are not any examples of this usage.
The reason for which it didn’t work in my project was my fault. I copy pasted the mount definition from my LiveView in my component. However, there is a subtle difference in the mount function for the LiveView and the one for the LiveComponent.

LiveComponent:
def mount(socket) do

LiveView:
def mount(_session, socket) do

So my mount definition was wrong and the value was never added to the assigns.

Thanks to everyone who took the time to help me.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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 52238 488
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement