cnck1387

cnck1387

Concrete examples of when to use live_patch, live_redirect, push_redirect, push_patch?

I’ve been reading the docs lately on LiveView and I’m having trouble figuring out when to use the above functions to change pages.

Can someone please provide a few real world use cases on when you should use a specific one and if possible how they would relate to using their regular non-LV counter parts.

It sort of feels like you might use live_redirect in a template when you want to go from page A to B, such as navigating between pages. I guess similar to link without LV (which is confusing to me because there’s also redirect without LV).

Then there’s live_patch for maybe modifying the state of an existing LV page, but this is where things fall apart in my head. When you would do this vs using live_redirect?

Then for push_redirect, this is probably the equivalent to using redirect without LV in a controller action, such as when you submit a form successfully right? But if that’s the case, why is live_redirect named that instead of something like live_link?

And for push_patch, I guess it’s similar to live_patch but used in the LV instead of a template?

Some clarification and guidance would be much appreciated. Especially if you can tie in specific use cases to specific function calls for things like nav bars, pagination, updating a tiny part of a page, various form actions (submitting with invalid fields vs successful), etc..

Most Liked

josevalim

josevalim

Creator of Elixir

At the end of the day, regardless if you invoke link/2, live_patch/2,
and live_redirect/2 from the client, or redirect/2, push_patch/2,
and push_redirect/2 from the server, the user will end-up on the same
page. The difference between those is mostly the amount of data sent over
the wire:

  • link/2 and redirect/2 do full page reloads

  • live_redirect/2 and push_redirect/2 reloads the LiveView but
    keeps the current layout

  • live_patch/2 and push_patch/2 updates the current LiveView and
    sends only the minimal diff

An easy rule of thumb is to stick with live_redirect/2 and push_redirect/2
and use the patch helpers only in the cases where you want to minimize the
amount of data sent when navigating within the same LiveView (for example,
if you want to change the sorting of a table while also updating the URL).

44
Post #9
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Notably, I’ve largely ditched the traditional view structure used by Phoenix controllers / views / templates in favor of:

my_app_web/
  pages/
    shipments/
      show/
        show.html.leex # the template
        show.ex # the live view
        data.ex # a data fetching helper
        view_helper.ex # the normal phoenix view helper
        components/ # components relevant only to this page
          ...

It makes this whole process way easier. I don’t really like Live in the module name anyway, it holds not a lot of value. The shipment show module name would be MyAppWeb.Pages.Shipments.Show, so it gets named just like any other Elixir module. All the files for a page are all in one nice spot, and you can make a new page by just copying and pasting any existing page folder.

sasajuric

sasajuric

Author of Elixir In Action

I only played briefly with live view, once about a year ago, and once in the past weekend, and I pretty much went for a similar structure, with the only difference being that I didn’t use Pages. This weekend, while experimenting, I used the name Controller for the live view module. So I ended up with something like MySystemWeb.PageA.Controller, MySystemWeb.PageB.Controller, etc.

I find it much saner to navigate through the code, and as you say the things which are frequently read or changed together are in the same place. I also think that the word Live is basically noise. Whether something is a live view or not is imo an implementation detail, and doesn’t belong to the module name.

In the future I plan to do the same thing with controllers. I already did something similar for my blog, but that’s a very simple case so I ended up consolidating everything behind a single controller.

Where Next?

Popular in Questions Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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
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
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
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
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
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

We're in Beta

About us Mission Statement