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.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!