Going Generic
In our case, what do generic Stimulus controllers look like?
What we have now is one controller that responds to a click by showing or hiding a target element and also by changing the text of a target.
Let’s think a bit more abstractly, and what we have here are two separate actions that respond to a click: one that adds and removes a CSS class and one that changes the text of a button. we’re going to split those into two separate controllers to show how Stimulus allows for composition of small, generic pieces of action.
Now, there’s an obvious downside here, which is that the code and the HTML are going to get more verbose. Let’s look at the code and then talk about the upsides.
Both the generic Stimulus controllers are mostly ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access