Using Values

Our code is pretty simple so far, but it’s still missing a bit of functionality. We’re not yet changing the text of the button from “Hide” to “Show.” On a more structural level, we don’t have a place in the code that explicitly stores the state of the button. We have to infer the state from the presence or absence of the hidden class, which is not a terrible thing to have to do, but it’s often better to explicitly have the state of the system available.

We can do all of these things using another Stimulus concept: values. In Stimulus, values are a bit of syntactic sugar that allow us to use data attributes to store data that is specifically associated with a controller, and gives the controller some methods to manipulate ...

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.