peter-de-boer

peter-de-boer

I have a stateful live component. Besides the id, it receives another parameter, “products”, from the parent live view.
The parent live view constains a pubsub subscription and an handle_info that changes the products parameter.
Always when products is changed, the live component is re-rendered (as expected), and re-mounted which is not as expected.

The documentation says, a.o. “In stateful components, mount/1 is called only once, when the component is first rendered. For each rendering, the optional preload/1 and update/2 callbacks are called before render/1.”

So, I would expect that update is called with the new products, parameter. But it appears that also mount is called. What am I missing?

Showing Posts 1 to 7

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @peter-de-boer, one explanation is that your live component is not actually stateful. Can you supply some code showing how you’re using it / setting it up?

peter-de-boer

peter-de-boer OP

My component:

defmodule BwWeb.CartComponent do
  use BwWeb, :live_component

  def mount(socket) do
    IO.puts("CartComponent mount")
    {:ok, assign(socket, show: false)}
  end

  def update(
    %{
      id: _id,
      cart_products: cart_products,
      cart_id: cart_id,
      store_id: store_id,
      store_slug: store_slug,
      show_checkout_button: show_checkout_button,
    } = _assigns, socket) do
    {:ok, assign(
      socket,
      cart_products: cart_products,
      cart_id: cart_id,
      store_id: store_id,
      store_slug: store_slug,
      show_checkout_button: show_checkout_button
    )}
  end

The component is called in the live view template:

   <%= live_component(
      @socket,
      BwWeb.CartComponent,
      id: Ecto.UUID.generate(),
      cart_products: @cart_products,
      cart_id: @cart_id,
      store_id: @store.id,
      store_slug: @store.slug,
      show_checkout_button: true
    ) %>

In the live view I handle a pubsub notification:

  def handle_info({Bw.Man.Cart, [cart_id, _], _}, socket) do
    IO.puts("handle_info")
    cart_products = Bw.Man.Cart.get_cart_products(cart_id, socket.assigns.store.id)
    {:noreply, assign(socket, cart_products: cart_products)}
  end

Always when this handle_info is executed, the CartComponent is mounted.

derek-zhou

derek-zhou

You updated one of the parameters, cart_products, so t has to be remounted. If you don’t want the remount, you have to send whatever thing you want to change via send_update

peter-de-boer

peter-de-boer OP

Thanks, I will try that. I guess I just do not understand the documentation as it seems to suggest that these kind of updates are handled by the update function.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This is your issue. This will generate a new UUID on every parent render which basically means you are unmounting the old component and remounting a new component on every render. Impure functions should generally be avoided in the EEX templating calls.

derek-zhou

derek-zhou

I’d even suggest you to avoid using stateful component until you are very comfortable with state-less components. Remember there is nothing you cannot do with a live view and a bunch of stateless component. Stateful component is only a feature to improve code organization; and you can improve code organization in other ways.

peter-de-boer

peter-de-boer OP

Awesome, that’s it!

(And I thought I was clever by generating some random id…)

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews