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 Phoenix.LiveView — Phoenix LiveView v1.2.5 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.

https://github.com/paulstatezny/live-view-back-button

Last Post!

paulstatezny

paulstatezny

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

https://github.com/paulstatezny/live-view-back-button

Where Next?

Popular in Discussions Top

AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14350 124
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New

Other popular topics Top

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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
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
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement