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.

forgo.unmount() not called after removing element

See original GitHub issue

The unmount() function does not call when unmounting a component. Sandbox

import * as forgo from "forgo";

const App = () => {
  const component = new forgo.Component({
    render() {
      return <p>Tooltip</p>;
    }
  });

  component.unmount(() => alert("unmounted!"));

  return component;
};

forgo.mount(<App />, document.getElementById("root"));

// forgo.mount(null, document.getElementById("root"));
document.body.removeChild(document.getElementById("root"));

I’m not entirely sure if either method correctly unmount a Forgo component.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
chronoDavecommented, Aug 7, 2022

Thank you for the quick response, I managed to adapt your example into a small helper. It works great 😄

export default (anchor: HTMLElement, element: forgo.ForgoComponent<any>) => {
  let unmounted = false;
  let unmount: () => void;

  const portal = () => {
    const component = new forgo.Component({
      render() {
        if (unmounted) return null;
        return element;
      }
    });

    unmount = () => {
      unmounted = true;
      component.update();
    };

    return component;
  };

  anchor.appendChild(forgo.render(forgo.createElement(portal, {})).node);
  return () => unmount();
};
1reaction
spiffytechcommented, Aug 7, 2022

Right now it’s presumed that unmounts happen when a Forgo render detects that a previously-displayed component stops being displayed. That works fine inside the Forgo app, but isn’t helpful if something outside Forgo needs to trigger an unmount of the whole app.

Forgo doesn’t have a solution in place for that yet because you’re the first person we’ve heard from who has integrated Forgo with other apps (vs greenfield work done entirely in Forgo).

For the moment, a hacky workaround would look like this:

import { rerenderElement } from "forgo-powertoys";

const App = () => {
  const component = new forgo.Component({
    render() {
      return <p>Tooltip</p>;
    }
  });

  component.unmount(() => alert("unmounted!"));

  return component;
};

const AppWrapper<{showTheApp: boolean}> = () => {
  return new forgo.Component<{showTheApp: boolean}>({
    render({showTheApp}) {
      if (!showTheApp) return null;
      return <App />;
    }
  });
};

forgo.mount(<AppWrapper showTheApp={true} />, document.getElementById("root"));

rerenderElement(document.getElementById("root"), {showTheApp: false);
document.body.removeChild(document.getElementById("root"));

Basically you put all the app logic that needs to care about the unmount lifecycle inside a component that you can rerender to unmount all of its decendants.

That’s really ugly, but it’ll at least get you moving today.


I’m looking into how practical it would be to expose a forgo.unmount(el) function for external logic to call, that would do all the teardown and remove the descendants of el. If that’s straightforward, I can get that implemented and published promptly.

Longer term, we’ll need to integrate with MutationObserver to allow arbitrary DOM operations on anything Forgo renders. But that’ll be a lot more complicated to implement safely, so I don’t expect to push that out soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Delete an Item from Array not Rendering ReactJS Hooks
You are rendering data from props.listofapi but updating array, your changes don't get updated because you are changing the wrong array.
Read more >
Toggle called from unmounted Tooltips · Issue #1255 - GitHub
Error message is being printed in the console because setState is being called on an unmounted component (UncontrolledTooltip). What should be ...
Read more >
React - How to Check if a Component is Mounted or Unmounted
This is a quick post to show how to track the mounted status of a React component so you can check if it's...
Read more >
Remove element from DOM in React way - DEV Community ‍ ‍
We will be using useEffect Hook to execute JavaScript Window.setTimeout() function which will help hide an element in the DOM (a side effect)....
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
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