question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change Callback Not Firing as Expected

See original GitHub issue

I’m new to the Stimulus library so apologies for the basic question. I have a controller with a version value that is mutated by a radio button list in a form. When the value of the radio button changes, I change version to what the user selected. Then I have a change callback function declared that executes the important logic I want to happen, in this case changing the image source for an img tag. Currently, the change callback only fires the first time the controller is connected, but never again.

I have observed that the data attribute in the HTML is changing as expected, but the change callback function is only called the first time.

Controller Code

import { Controller } from "@hotwired/stimulus"

const imageMap = {
  11: '...',
  12: '...',
}

export default class extends Controller {
  static targets = [ 'heroImage' ];
  static values = { version: Number }

  changeVersion(event) {
    this.versionValue = Number(event.currentTarget.value);
  }
  
  versionValueChanged(version) {
    this.heroImageTarget.src = imageMap[version]
  }
}

HTML

<div 
  data-controller="pdp" 
  data-pdp-version-value="12"
>
  <form>
    <% (11..12).to_a.reverse.each do |v| 
      id = "#{v}th-gen"
    %>
      <div>
        <input 
          type="radio" 
          id=<%= id %>
          name="generation" 
          value=<%= v %>
          data-action="change->pdp#changeVersion:prevent"
          <%= v == 12 ? 'checked' : nil %>
        >
        <label for=<%= id %>><%= "#{v}th Generation" %></label>
      </div>
    <% end %>
  </form>
  <img 
    data-pdp-target="heroImage"
    src="..." 
  />
</div>

Any help or suggestions to fix this? I think I’m using the change callback as intended.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
arbcommented, Aug 15, 2022

@tleish and @marcoroth - I can confirm that the pen example does indeed work as expected. I’ve been testing in Chrome and Safari.

Interestingly, if I copy and paste in the controller code provided by @tleish in my local project, the change callback fires as expected with no changes to my HTML. I actually can’t spot a meaningful difference between my controller code and the controller code in the pen.

I just tried again locally with my original code and now it does seem to be working as the documentation describes. I’m not entirely sure what changed or why it’s working now, but it’s working enough to close the issue! Thanks for all the input everyone, it’s appreciated!

0reactions
marcorothcommented, Aug 15, 2022

Awesome @arb, thanks for the feedback!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - DependencyProperties not firing callbacks as expected in ...
I am binding this property to a DP (called Test) in MainWindow which seems to be firing the OnPropertyChanged events every time it...
Read more >
Change Callback Not Firing as Expected - Hotwire Discussion
I have observed that the data attribute in the HTML is changing as expected, but the change callback function is only called the...
Read more >
Dependency PropertyChanged CallBack is not firing - MSDN
Hi, I am facing a weird issue in Dependency property changed call back. <Bing:BingMap x:Name="BingMapUC" Bing:MapDP.
Read more >
[any-plat] DependencyProperty changed callback not firing ...
Current behavior DependencyProperty Changed event not proc'ing when the dp is set from style setter. Expected behavior Should work just like ...
Read more >
Timer Mocks - Jest
At this point in time, the callback should not have been called yet expect(callback).not.toBeCalled(); // Fast-forward until all timers have ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found