Learn Stimulus fast. This is a quick overview of how to use Stimulus core features compressed into one minute. Remember to pause the video if you need time to understand something!
This lesson uses Stimulus v2.
To learn more about Stimulus and the rest of the features we haven't covered, visit the official website.
Rodrigo Pombo: [0:00] To make a piece of HTML interactive, you need to create a controller class and link an element to the controller with the data-controller attribute. You can use the data-action attribute, for example, to handle click events. Specifying the name of the controller and the method, Stimulus will call the method on every click.
[0:20] To add a state, you can add a property to the controller to display it. First, you need a reference to a DOM element. Stimulus calls those references targets. After you define the target, you can access the DOM element as a controller property. Now every time the state changes, Stimulus re-renders the component.
[0:45] To have two of these buttons, you can duplicate the HTML and reuse the controller. If you add an attribute and apply it as a value in the controller, you can access it as a controller property similar to targets.