Coder Social home page Coder Social logo

Comments (6)

spiffytech avatar spiffytech commented on September 28, 2024 1

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.

from forgo.

chronoDave avatar chronoDave commented on September 28, 2024 1

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();
};

from forgo.

spiffytech avatar spiffytech commented on September 28, 2024

Ah, you're bypassing Forgo's teardown logic by nuking the element with the DOM APIs. Forgo doesn't handle that right now. Supporting it has been on our long-term roadmap.

Let me think on what it would take to implement that.

from forgo.

chronoDave avatar chronoDave commented on September 28, 2024

What would be the correct way to unmount a component?

from forgo.

spiffytech avatar spiffytech commented on September 28, 2024

I've got a PR up that adds a forgo.unmount() function.

I'll want to update the docs separately - I need to figure out what the right way to unmount on page exit is, and at a quick glance all I see are browser events labeled "deprecated" and "don't do this". I'll have to check what other frameworks do / recommend.

from forgo.

spiffytech avatar spiffytech commented on September 28, 2024

This has been merged and will be released in v3.2.1.

from forgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.