Sanjibukai

Sanjibukai

How do you handle "back button" without having the LiveView mounting again?

Hello everybody,

I have an application where I have many LiveComponents within the same Liveview and the same route/url.
So I’m managing manually all the steps and states of components…

Now I have a use case where I can display a TOS in full page like a modal from any LiveComponent.
I could have managed it the same way (though it’s a bit overwhelming to manage all the steps manually)…
But I wanted to let the user be able to dismiss the TOS using it’s browser back button (particularly convenient for mobile with swipes).

So the only solution I found for that was to use a dedicated route and using live_patch so that I can come back to the previous route.

However, I just noticed that this trigger a whole new mounting, thus losing every state.

I precise that the TOS and the LiveComponents are within the same LiveView.
If I put a live_patch link to the previous route manually for testing purpose, I have the states kept correctly.

Now I can track manually every route/url of the pages where the user is before displaying the TOS so that I can redirect (with live_path) him back to where it was. But that will not solve the browser back button issue.

While I’m here isn’t there a way to keep track of the previous path by default in the socket?

Anyway, maybe I’m doing something wrong with how I organize my LiveView/LiveComponent…

How would you do that?

I’m thinking of going back to handle every view on theirs own routes but it was cool to have everything done in the same route (for related actions)

Marked As Solved

Sanjibukai

Sanjibukai

Ok,

I figured out that I will need to push_patch https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#push_patch/2 to be able to let the browser go back without mounting.

It’s very interesting…

Also Liked

paulstatezny

paulstatezny

By the way, I created an example Phoenix application to observe LiveView’s behavior for push_patch and push_redirect.

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New

Other popular topics Top

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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
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
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47849 226
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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