Live view problem - value not increasing on click

Hey guys I begin with phoenix LiveView so I wanna try something but it doesn’t work.

There is my code in page live:

def mount(_params, %{}, socket) do
    categories = CatRequette.get_all_categorie()
    {:ok, socket |> assign(categories: categories, panier: 0, search: nil)}
  end

  def handle_event("add", %{}, socket) do

    {:noreply, update(socket, :panier, &plus/1)}
  end

  defp plus(x) do
    x + 1
  end

  def render(assigns) do
    BebemayotteWeb.PageView.render("contact.html", assigns)
  end

and in my page html.leex:

<input type="text" name="valeur" value="<%= @panier %>" >
        <button phx-click="add">+</button>

When i click the button, it should increase the value of @panier but it doesn’t work like that.

What 's wrong

use assign to update the assigns.
Here I assume you got some helper function to increase the value.

def handle_event("add", _, socket) do
    panier = inc_panier(socket)
    {:noreply, assign(socket, :panier, panier)}
end

The code you have looks largely correct actually, I think @sebb is wrong, update is also valid. However, are you sure your handle_event clause is firing?

1 Like

It looks fine. Does the log report any errors when you click the button? Or does nothing happen at all?

@benwilson512, It is there that I do not know how to rectify the problem because exactly that does not enter in the function !!!

try enable liveview debug from the javascript console in your browser:

liveSocket.enableDebug()

I suspect there is something wrong with the websocket connection so you only see the initial “dead” view.

1 Like