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.

Events Don't Get Removed When Re-Using An Element

See original GitHub issue

The Issue

Event listeners (and maybe other attributes, not sure) don’t get removed when re-using an element.

Broken Code <>

const { h, app } = hyperapp
/** @jsx h */

app({
  model: true,
  actions: {
    toggle: model => !model
  },
  view: (model, actions) =>
    <div>
      {model && <div onClick={() => alert('🐛')}>a</div>}
      <div>b</div>
      <button onClick={actions.toggle}>toggle</button>
    </div>
})

How to reproduce with the codepen?

  1. Click a. Notice how it properly alerts 🐛
  2. Click b. Notice how it doesn’t do anything
  3. Click toggle
  4. Click b. WTF?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jorgebucarancommented, Mar 16, 2017

I think is fixed in master.

1reaction
rbiggscommented, Mar 13, 2017

This is why I don’t like inline events. Event delegation solves this:

app({
  model: true,
  actions: {
    toggle: model => !model
  },
  view: (model, actions) =>
    <div id='parent'>
      {model && <div class='clickable'>a</div>}
      <div>b</div>
      <button onClick={actions.toggle}>toggle</button>
    </div>,
  
  subscriptions: [
    function() {
      // Define event on clickable parent.
      document.querySelector('#parent').addEventListener('click', function(e) {
        // Fire event when event target is "clickable".
        if (e.target.className === 'clickable') {
          alert('🐛')
        }
      })
    }
  ]
})

With event delegation you can add and delete event targets as you please without worrying about event management.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do event listeners disappear when reusing a fragment?
Well yeah I know that, that's why I'm reusing the fragment which has an event listener already attached. I'm saying that the event...
Read more >
Reuse interactions | Webflow University
Reusing interactions on a series of similar components requires you to replace elements from your original interaction with elements you're trying to ...
Read more >
How to Reuse Virtual Event Elements | BeyondLive
Once your online event is over, some elements will no longer be relevant. Updating the website is important to remove elements such as...
Read more >
Introduction to events - Learn web development | MDN
This might be a single element, a set of elements, the HTML ... Then the event handler created by the code above can...
Read more >
Activation events - Optimize Resource Hub - Google Support
To activate your Optimize experience using an activation event, click Page load [ ... Only newly detected instances of an element are modified...
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