Access to @socket from within function component

I have the following function component:

# dashboard_live.ex
  def user entry(assigns) do
    ~H"""
        ...
        <.link patch={Routes.dash_path(@socket, :show_user, @user.id)}>
          Username
        </.link>
       ...
    """
  end

The problem is that @socket is not passed to components it seems, so usual link generation doesn’t work. How would one go about generating links with function components?

1 Like

You could pass the @socket in or use MyAppWeb.Endpoint

Option 1)

<.user_entry user={@user} socket={@socket} />

Option 2) - Replace MyAppWeb with whatever your app is

  def user_entry(assigns) do
    ~H"""
    ...
    <.link patch={Routes.dash_path(MyAppWeb.Endpoint, :show_user, @user.id)}>
      Username
    </.link>
    ...
    """
  end

These path helpers can accept Endpoints as well as socket/conn

2 Likes