annad

annad

Is this right? send self() AND send_update to push data up to parent

VERSIONS: phoenix 1.6.6 phoenix_live_view 0.17.9

CONTEXT: I have a child component that collects country, state_region and city data and returns a geo_point (with timezone, latlong, etc). The parent can either be a LiveView OR another component.

ISSUE: Using send self() always sends the data up to the LiveView, but I need it to sometimes go to another component. So now I have the code below to switch between send self() and send_update depending on the parent_type. It also means that to use this component, the parent liveview or component must set a “parent_type” assign of either “component” or “liveview.”

This works but seems a bit clunky to me. Am I doing this right?

defp notify_parent(socket) do

    data =  { %{"geo_status" => socket.assigns.geo_status,
                "geo_point" => socket.assigns.geo_point,
                "country" => socket.assigns.country,
                "state" => socket.assigns.state,
                "city" => socket.assigns.city }}

    case socket.assigns.parent_type do
      "liveview" -> send self(), {:geo_data, data}
      "component" ->  send_update(socket.assigns.module,
                      id: socket.assigns.parent_id,
                      update: :geo_data, data: data)
    end
    socket
  end

Most Liked

trisolaran

trisolaran

This works but seems a bit clunky to me. Am I doing this right?

I would say it’s very clunky. A component shouldn’t have to know who its parent is in order to communicate with it, there should be one API. But, as @benwilson512 said, this is currently the right way.

Unifying the API for communicating with LiveViews and LiveComponents is AFAIK ongoing work and was mentioned by Chris McCord during his talk at Elixirconf 2022.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This is indeed a bit clunky, but it’s also correct as far as I know.

c4710n

c4710n

I did it the same way.

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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

We're in Beta

About us Mission Statement