trentjones21

trentjones21

Live_redirect from a child rendered LiveView

I have multiple LiveView pages (live routes) that all need to render a child LiveView (a date picker for filtering data by date range). I want this child LiveView to be able to use live_redirect in order to change the query params in the url when it is interacted with. However, I’m getting an error saying you can’t use live_redirect in a child rendered LiveView.

I’m pretty new to Elixir, and I can’t find a lot of information on how to accomplish this. I’ve seen other threads saying to use pubsub, but it seems like that is going to lead to a lot of duplicate code as each “parent” component is going to have to subscribe to messages from the child (at least that is my understanding).

Aniy tips on how I can use live_redirect when something happens in a child rendered LiveView?

Marked As Solved

josevalim

josevalim

Creator of Elixir

You have to message the parent because the URL is a “single resource”, and if you have two things trying to change it in incompatible ways, your application goes into an inconsistent state.

It doesn’t know to be “use pubsub”, you could do it like:

send(socket.root_pid, {:redirect_to, Routes.foo_bar(...)})

And then handling it in your parent handle_info, but in a nutshell, you should double think if this approach is correct. In particular, double think if you really need two live views (especially with the components features in master - although you will need to check the source to read the docs).

Also Liked

trentjones21

trentjones21

Thank you for the response. It was really helpful.

I realized my mistake. In case anyone ends up here with the same problem I had – I was treating live views much like one would treat react components. However, it occurred to me after reading Jose’s response that I could make a regular .eex component rather than using a live view to accomplish what I was trying to do. I ended up following this tutorial and got things working how I needed them, without having to use a child live view.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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

We're in Beta

About us Mission Statement