Hey HN!<p>For those new to Rails, Stimulus (<a href="https://stimulus.hotwired.dev/" rel="nofollow noreferrer">https://stimulus.hotwired.dev/</a>) is a way to sprinkle frontend javascript code into your Rails views.<p>I held off using Stimulus in my Rails apps for a long time because I didn't understand it — the data= attributes looked weird, and the connection between my Rails views and Stimulus javascript controllers was confusing.<p>So I decided to write the guide I wish I had when I was learning Stimulus.<p>Hope you enjoy it! I'm happy to answer any questions, feel free to ask any.<p>Thanks, Harrison
I love stimulus. And Hotwire. I find everything more maintainable and faster to developer when rails on the centre of the universe. Obviously a personal preference.<p>I faced a similar display toggle-able use case for form fields recently.<p>I did find that I used stimulus to hide the fields there was a slightly “flicker” as they appeared and then were hidden in the connect() function.<p>It didn’t affect my use case, as I could default hide and then toggle them to appear with stimulus.<p>But in the event they should appear by default and then be hidden based on some logic within the stimulus controller, is there a better way to handle this?
Thanks for the writeup. I had the opportunity to use StimulusJS at a recent opportunity and couldn't agree with it. I found several issues with the patterns it encourages, such as:<p>-magic variable transformed from props, lots of uncertainty between the HTML attributes I typed and the actual name of the JS variable
-have to now define these magic variables in typescript
-defining values and automatically converting strings / numbers magically seems hacky
- why does it ask to separate CSS classes into it's own special attribute and magic variable?<p>I feel like I'm speaking crazy when I see anyone recommend Stimulus and from my experience I'd much rather just use web components without shadow DOM. Seriously, EVERYTHING Stimulus does web components do 10x better and cleaner. They both try to do the same thing but the Stimulus approach honestly came across as a high school final project taken too far.