A live component and the query string

Hi all,
this may sound silly but I have some doubts regarding a live component and the query params.

I want to develop a live component (which is a list of items) that would keep as its state the items and let’s say the ordering.

I want my component (or the live view?) to update the query string when the user change the ordering.

Now, my doubt is: who’s responsible for updating the query string? The component or the live view?

My goal is to reuse the component in multiple pheonix projects, so I would like to have it as much self-contained as possible.


URL handling is always the responsibility of the (root) LV. By definition there’s just a single URL, so you need to have a single place to coordinate changes to it. It makes the most sense to do that in the root LV, which also has the handle_params callback for reacting to url changes.

1 Like

Which means I have to write the logic to handle the query string in all the live views that will use the component.

Should my component provide helpers for that?
Like, I don’t know, a function that takes the params and return an updated version with the sorting?
Should my component emit events on the live view when it updates the sorting?

My goal is to have a plug and play component, what is the best strategy to achieve that?

I would personally try that approach. And at the same time, if needed, the component could provide some helper to allow the LiveView to easily convert the sorting into a query string