cnck1387
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..
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
nickdichev
I’m interested in this as well, I haven’t been following LiveView too closely and haven’t worked with these functions too much.
For
push_patchhowever, I do have an example. You’re correct that it is used on the server side to update the state of the LiveView. An interesting trick I learned in this blog post was usinghandle_params/3along with a server side state change to generate modals.The blog post hasn’t been updated for the navigation changes in LiveView 0.7 but I have updated my implementation to use the new navigation functions. This example app is deployed and linked on the Github, you just have to manually navigate to the
/room/<room_name>/adminroute to see the modals in action…cnck1387
Also, on a related topic, when should you use
live_componentvs making a regular live view, and are live components without state the same as template partials in a non-LV app?So many questions! Answers related to practical examples would be great. The docs are very much written from a POV of describing the API, not applying it. It makes it kind of hard to understand while learning LV.
benwilson512
This is also basically a question I have. Live view applications can totally use regular partials, so when is it important to use a stateless component vs a regular partial? Is it purely an optimization / code organization thing?
cnck1387
Do you use
renderin that case and put the partial in yourtemplates/directory? This wasn’t ever mentioned in the docs. I didn’t even think it was possible, because we havelive_rendertoo, I thought that was supposed to replacerenderto some extent.benwilson512
It is 100% possible, and it is 100% unchanged from ordinary views.
live_renderis not a replacement forrenderat all.live_renderis used when you want to embed a new / sub liveview in a page.nubunto
This is something on the back of my mind as well, and these are my thoughts after having implemented an LiveView application for a hackathon this past weekend.
As all good answers, it depends. I’ll first explain a bit of what I did and how I structured it, and then we’ll discuss some use cases I came up with while tinkering with LiveView/Components.
Disclaimer: it’s my first experience with LiveView so I’m open to scrutiny/suggestions! Take this with a grain of salt
What I did was basically a full blown single-page-application with 5 screens. I used the LiveView layout to render a
live_componentthat contained thelive_redirectto 5 different LiveViews.I feel that you should reach for a LiveComponent when you want to separate a complex piece of UI from the main LiveView and you need to share data between the main LiveView and the Component. I say this because the above could have been achieved without using LiveComponent at all through
render/2as stated in LiveView docs, my most viewed and loved page of the weekend <3.Why did I use LiveComponent? Well, I overlooked this and LiveComponent just worked, so I moved on. But in hindsight, it made more sense to use
render/2for this particular example. However, the advantage of using LiveComponent is it’s hability to handle events and have state!Think of LiveComponents as a kind of function that encapsulates a complex piece of UI under a parent LiveView. They can be stateless or stateful, but they are always tied to a parent LiveView. This complicates things a little bit, since you can send data up to the parent.
Most of the time, you’ll want to pass data from the LiveView down to the LiveComponent. This is how I learned in React, and imo makes your interactions easier to reason about. However, unlike React, Phoenix gives you a “event bus” through PubSub! So you could trigger changes using PubSub and read those changes up in the parent LiveView. These are all documented in the LiveComponent docs.
If you’re familiar with React, you can think of LiveComponents as being React components. LiveComponents with an
:idare stateful, akin to React components that usesetStateoruseState, and LiveComponents without an:idare akin to “dumb” React components, that is, components with no state.Tl;dr: IMO a LiveView should have one concern to worry about. Group functionality that is closely related domain-wise under a LiveView, and use LiveComponents to handle complex piece of functionality under your LiveView.
live_redirectmeans changing domains, i.e. is related to other LiveViews, andlive_patchmeans changing functionality in the same LiveView, i.e. is related to LiveComponents.josevalim
Partials and stateless components are pretty much the same.
josevalim
At the end of the day, regardless if you invoke
link/2,live_patch/2,and
live_redirect/2from the client, orredirect/2,push_patch/2,and
push_redirect/2from the server, the user will end-up on the samepage. The difference between those is mostly the amount of data sent over
the wire:
link/2andredirect/2do full page reloadslive_redirect/2andpush_redirect/2reloads the LiveView butkeeps the current layout
live_patch/2andpush_patch/2updates the current LiveView andsends only the minimal diff
An easy rule of thumb is to stick with
live_redirect/2andpush_redirect/2and 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).
cnck1387
Where does
mountcome into play betweenredirectandpatch?For example, in the docs I read
mountonly gets executed once when loading a LV, and if you patched between content A and B in the same LV (such as your sorting example), mount wouldn’t get run a 2nd time.But does that mean it’s not possible to
live_patchbetween 2 different LVs since each of them have their own mount? How would that work in practice?Is there anything worth thinking about for the “pretty” part of that. Like, performance wise or any gotchas? Also from the docs, it’s not clear where the partial’s file should live. Should it be in the same directory as the LV? Typically partials are in the templates directory.
josevalim
It is remounted on redirect, not remounted on patch. That’s the main distinction between them.
So you can’t patch across different LiveViews. If the client emits a patch but it is another LiveView, it falls back to a redirect.
They have to be in the templates directory, since the partial belongs to a View (and not the LiveView). I will clarify this.