Accessing the URL in a child LiveView

I have a situation where the left navigation of my application needs to subscribe (PubSub) to updates in the system and re-render. Since this navigation is on every page, what seems like the most realistic approach is to make the left navigation a child LiveView with live_render - since I don’t want every page/LiveView needing to handle these messages.

The problem however is I can’t figure out how to get the url or @live_action (for setting the active page class) to this child LiveView. The things I’ve tried but have failed due to:

  • Child LiveViews can’t handle_params.
  • Child LiveViews @live_action is always nil
  • Child LiveViews can’t read connect_params
  • Child LiveViews can only be passed data through the session which is only set once at mount (so doesn’t update when navigating)

Am I stuck just making this left nav a component and maybe use ing the handle_info callback in to each LiveView? Am I going about this completely wrong?

1 Like

You could use a stateful component, that way you can subscribe in the mount and keep refreshing it within the component. That way you would not have to redefine the same data in every liveview to pass down, you would just keep state in the component.

Whoops - deleted my post trying to edit it.

The problem is if it’s a component, stateful or not, the parent LiveView would have to implement the handle_info callback (Since I want the component to PubSub subscribe to a topic). And since the component would be part of the layout - every LiveView/page would need to implement those handle_info callbacks. This isn’t a problem per-se, I can macro them in with use, I just liked the idea of keeping the state fully encapsulated as a child liveview. The only thing preventing me from doing so is this issue with not having access to the url / action so I can render the navigation correctly.