Connected and Disconnected Target Callbacks with Stimulus

This article was originally published on Rails Designer—UI components library for Rails app, built with ViewComponent, designed with Tailwind CSS and enhanced with Hotwire.

In a previous article I wrote about the change callbacks for the Values API…


This content originally appeared on DEV Community and was authored by Rails Designer

This article was originally published on Rails Designer—UI components library for Rails app, built with ViewComponent, designed with Tailwind CSS and enhanced with Hotwire.

In a previous article I wrote about the change callbacks for the Values API. This time I want to highlight the Connect and Disconnect callbacks for targets.

A quick recap on targets: targets lets you reference important elements by name. They can be set up like so:

<div data-controller="eggs">
  <h2>Eggs</h2>

  <ul>
    <li data-eggs-target="item">🥚</li>
  </ul>
</div>

Then in your Stimulus controller:

// app/javascript/controllers/eggs_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["item"];
  // …
}

Keep track of new targets being added

Let's assume above eggs list only allows you to add more eggs, nothing else. 🐣 The HTML could look like this:

<div data-controller="eggs">
  <h2>Eggs <span data-eggs-target="count"></span></h2>

  <ul data-eggs-target="list">
    <li data-eggs-target="item">🥚</li>
  </ul>

  <button data-action="eggs#add">Add</button>
</div>

Let's quickly extend the above controller with the add feature:

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["list", "item"];

  add() {
    this.listTarget.insertAdjacentHTML("beforeend", `<li data-eggs-target="item">🥚</li>`);
  }
}

Easy enough! Let's write the code to show the number of eggs in the list.

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["list", "item", "count"];

  // …

  itemTargetConnected() {
    this.#updateItemCount();
  }

  itemTargetDisconnected() {
    this.#updateItemCount();
  }

  // private

  #updateItemCount() {
    this.countTarget.textContent = `(${this.itemTargets.length})`;
  }
}

Preview of the controller, showing adding the egg-emoji to a list

Notice how the <span data-eggs-target="count"></span> gets updated with the number of eggs (targets) whenever a new one is added to the list. Also notice how it is also already updated upon the controller connect lifecycle method (ie. when the page is loaded).

Yet another small, but really useful lesser known feature of Stimulus.


This content originally appeared on DEV Community and was authored by Rails Designer


Print Share Comment Cite Upload Translate Updates
APA

Rails Designer | Sciencx (2024-09-10T13:00:00+00:00) Connected and Disconnected Target Callbacks with Stimulus. Retrieved from https://www.scien.cx/2024/09/10/connected-and-disconnected-target-callbacks-with-stimulus/

MLA
" » Connected and Disconnected Target Callbacks with Stimulus." Rails Designer | Sciencx - Tuesday September 10, 2024, https://www.scien.cx/2024/09/10/connected-and-disconnected-target-callbacks-with-stimulus/
HARVARD
Rails Designer | Sciencx Tuesday September 10, 2024 » Connected and Disconnected Target Callbacks with Stimulus., viewed ,<https://www.scien.cx/2024/09/10/connected-and-disconnected-target-callbacks-with-stimulus/>
VANCOUVER
Rails Designer | Sciencx - » Connected and Disconnected Target Callbacks with Stimulus. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/10/connected-and-disconnected-target-callbacks-with-stimulus/
CHICAGO
" » Connected and Disconnected Target Callbacks with Stimulus." Rails Designer | Sciencx - Accessed . https://www.scien.cx/2024/09/10/connected-and-disconnected-target-callbacks-with-stimulus/
IEEE
" » Connected and Disconnected Target Callbacks with Stimulus." Rails Designer | Sciencx [Online]. Available: https://www.scien.cx/2024/09/10/connected-and-disconnected-target-callbacks-with-stimulus/. [Accessed: ]
rf:citation
» Connected and Disconnected Target Callbacks with Stimulus | Rails Designer | Sciencx | https://www.scien.cx/2024/09/10/connected-and-disconnected-target-callbacks-with-stimulus/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.