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 ...
Get Modern Front-End Development for Rails, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.