solop

solop

There are javascript hooks for mount, update, delete etc, but none specifically for insert.
Mount is called on each item when the page is first loaded, the proposed “insert” hook would only be called if a new item is inserted into the stream.

Showing Posts 1 to 2

sodapopcan

sodapopcan

Hooks are about generic JS interop and not directly related to streams. The lifecycle events you describe are directly related to the node the hook is attached to, not its children.

That said, how do you have your hooks set up?

If your hook is attached to each item in the stream, then mounted is exactly what you want. mounted is called any time an element with an attached hook is added to the DOM, not just on page load.

If you have your hook on the stream’s container, then a custom event can get you what you’re looking for:

Hooks.StreamContainerHook = {
  mounted() {
    const streamContainer = this.el

    this.handleEvent("my-custom-event-name", ({ id }) => {
      const insertedNode = streamContainer.querySelector(`#stream_item_${id}`)
    }
  }
}
def handle_event("insert", _, socket) do
  new_item = %{id: 1, name: "Some item"}

  socket =
    socket
    |> stream_insert(:my_stream, new_item)
    |> push_event("my-custom-event-name", %{id: new_item.id})

  {:noreply, socket}
end
chrismccord

chrismccord

Creator of Phoenix

If you put a hook on the stream items, the mounted callback would be fired for each added item, so you effectively have what you’re asking for already :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Proposals: Ideas Top

Stef-Verniers
I’m a bit in a fuzz about this topic. I’ve worked out some sort of Dropbox feature for a specific client. This feature allows him to uplo...
New
a3kov
I recently learned about this API and was surprised there’s no explicit support for it in the the Phoenix. Example use cases: A webs...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews