I’m a new Stimulus user, following the handbook.
I cloned stimulus-starter to work within my own editor. All went fine until ‘Is This Thing On?’.
Clicking the ‘greet’ button gave no response in the console. The connect method in the hello controller doesn’t seem to trigger.
Any suggestions on how to identify the problem?
Thanks
Daniel
Is there a console output when you reload the page? Connect method should run when the controller is connected to the DOM. You have nothing attached on your <button> so it doesn’t do anything.
No, the console is empty (and stays empty on reload).
The handbook says:
Next, we need to tell Stimulus how this controller should be connected to our HTML. We do this by placing an identifier in the data-controller attribute on our <div>
Which I’ve done.
As far as I can see I’ve followed the handbook instructions precisely (twice), but get no console output.
This might not be relevant here (I’m not familiar with the Starter project you’re referring to). But, when I first started looking into Stimulus (I’m new here), I didn’t understand how Stimulus mapped the data-controller value to the controller file seeing as any bundling would remove individual files.
Eventually, I read that the Ruby on Rails packaging does some automagic mapping. And, that if I wasn’t using the RoR asset bundling (which I’m not), I would have to explicitly tell Stimilus which identifier mapped to which JavaScript class.
It looks like you’re not actually doing anything in your connect method. Do you get any logging in your console when you replace the connect with this code?
From the code sample, I can identify several areas to look at.
The hello controller is created twice. Once in src/controllers/hello_controller.js and once in the HTML file Stimulus.register("hello", class extends Controller
It appears you may be using webpack from import { definitionsFromContext } from “@hotwired/stimulus-webpack-helpers”, however the HTML file source does not include the output of the file generated by webpack, so index.js and hello_controller.js are not loaded.