Lifecycle Callbacks

Special methods called lifecycle callbacks allow you to respond whenever a controller connects to and disconnects from the document.

import { Controller } from "stimulus"

export default class extends Controller {
  connect() {
    // …


You may define any of the following methods in your controller:

Method Invoked by Stimulus…
initialize() Once, when the controller is first instantiated
connect() Anytime the controller is connected to the DOM
disconnect() Anytime the controller is disconnected from the DOM


A controller is connected to the document when both of the following conditions are true:

When a controller becomes connected, Stimulus calls its connect() method.


A connected controller will later become disconnected when either of the preceding conditions becomes false, such as under any of the following scenarios:

When a controller becomes disconnected, Stimulus calls its disconnect() method.


A disconnected controller may become connected again at a later time.

When this happens, such as after removing the controller’s element from the document and then re-attaching it, Stimulus will reuse the element’s previous controller instance, calling its connect() method multiple times.

Order and Timing

Stimulus watches the page for changes asynchronously using the DOM MutationObserver API.

This means that Stimulus calls your controller’s lifecycle methods asynchronously after changes are made to the document, in the next microtask following each change.

Lifecycle methods still run in the order they occur, so two calls to a controller’s connect() method will always be separated by one call to disconnect().

Next: Data Maps