Skip to content

renderComponent should automatically cleanup if the parentElement (the element passed to into) is removed from the DOM #21097

Closed

Description

@NullVoxPopuli

🐞 Describe the Bug

Currently, it looks like we'd use this

without this, users need to to manually call destroy (available on renderComponent's return value)

e.g.:

let { destroy } = renderComponent('...', { into: x });

// time passes

destroy();

vs

renderComponent('...', { into: x });

x.remove();

// component is destroyed for you (async)
// (desired, but not happening today)

This could also happen if you remove all of the children

x.innerHTML = '';

should trigger cleanup

Activity

  1. mtzamorim commented on Sep 7, 2026

    @mtzamorim

    I'd like to work on this issue. I'm planning to start by adding coverage for removing the into element and clearing its children, while preserving the existing detached-element behavior. Would a MutationObserver-based approach be aligned with the intended implementation?

  2. NullVoxPopuli commented on Sep 7, 2026

    @NullVoxPopuli
    SponsorContributorAuthor

    It is the only way, afaik

  3. NullVoxPopuli commented on Sep 14, 2026

    @NullVoxPopuli
    SponsorContributorAuthor

    here is the code you need to do just that in user space -- not sure it's a good idea to have the framework add MutationObservers for "just in case" scenarios

    Demo

    Code:

    import { renderComponent } from '@ember/renderer';
    import { 
      trackedArray, trackedObject 
    } from '@ember/reactive/collections';
    
    const stats = trackedObject({ added: 0, removed: 0 });
    const divs = trackedArray();
    const renders = new Map();
    
    const cleanerUpper = new MutationObserver((records) => {
      let keys = new Set(renders.keys());
    
      for (let record of records) {
        let removed = record.removedNodes;
    
        for (let removedDiv of removed) {
          
          if (keys.has(removedDiv)) {
            let renderResult = renders.get(removedDiv);
            if (renderResult) {
              renderResult.destroy();
              stats.removed++;
              console.log('cleaned up')
            }
          }
        }
      }
    });
    
    
    function addComponent() {
      let div = document.createElement('div');
      let result = renderComponent(Counter, { into: div });
      divs.push(div);
      stats.added++;
      renders.set(div, result);
    }
    
    function removeComponent() {
      divs.pop();
    }
    
      let panel = document.querySelector('.limber__output-panel');
      console.log('setting observer on', panel);
      cleanerUpper.observe(panel, { childList: true, subtree: true });
    
    function getStats() {
      return {
        added: stats.added,
        removed: stats.removed,
      }
    }
    
    <template>
      <button {{on 'click' addComponent}}>add component</button>
      <button {{on 'click' removeComponent}}>remove component</button>
    
      {{#each divs as |d|}}
    
        {{d}}
    
      {{/each}}
    
      <fieldset><legend>stats</legend>
        <pre>{{JSON.stringify (getStats) null 3}}</pre>
      </fieldset>
    </template>
    
    import Component from '@glimmer/component';
    import { tracked } from '@glimmer/tracking';
    
    class Counter extends Component {
      @tracked count = 0;
      increment = () => this.count++;
    
      <template>
        <button {{on 'click' this.increment}}>inc: {{this.count}}</button>
      </template>
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions