Mid-level Track Roadmap

StimulusJS: lifecycle

This course follows a Stimulus controller through its lifecycle callbacks, using controllers from one Rails app. It covers connect(), which runs when a matching element appears in the DOM, and disconnect(), which undoes what connect() started: a Chart.js instance, a pending timer or listeners on window. It explains why initialize() runs once per instance while connect() can repeat, how xxxValueChanged callbacks react to value changes, and how target callbacks track single targets. A closing lesson traces one element's full timeline and presents a Ruby challenge.

What you'll learn

  • ✓When Stimulus calls connect() and how to guard async work that runs after an await
  • ✓Using disconnect() to undo what connect() created, such as charts and timers
  • ✓Why listeners attached to window must be removed in disconnect()
  • ✓The difference between initialize(), which runs once per instance, and connect(), which can repeat
  • ✓How xxxValueChanged callbacks react to value changes without a reconnect
  • ✓How xxxTargetConnected and xxxTargetDisconnected track single targets inside a connected controller
Free preview · Lesson 1 4 min read

connect(): where a controller's real work begins

A Stimulus controller class exists as soon as its file loads: every method is defined, every static declaration is read. That doesn't mean the controller is doing anything yet. connect() is the method Stimulus calls when an element carrying this controller's identifier shows up in the DOM. This…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...