Giter Site home page Giter Site logo

Comments (7)

fahad19 avatar fahad19 commented on May 19, 2024

Hi @pinguxx! Thanks for the detailed issue with examples.

Proppy instances are meant to live and die with the Component's own lifecycle.

For example, in React:

  • Proppy factory (not instance) is attached to Component
  • Component is mounted, and its own lifecycle creates an instance of Proppy out of the factory (P)
  • When component is unmounted, the Proppy instance is also destroyed

What I see in your example, the Proppy instance is created outside of the component. That will lead to unexpected behaviour.

from proppy.

pinguxx avatar pinguxx commented on May 19, 2024

On the second like i was creating the instance outside the component but that sounded like its a bad practice... not sure how i should handle this case then, any ideas?

from proppy.

fahad19 avatar fahad19 commented on May 19, 2024

I have never used hyperhtml before, but I would like to know what are its lifecycle hooks?

Like React has componentDidMount and componentWillUnmount, what are the equivalent of them in hyperhtml?

Knowing about it will allow me to help you better.

from proppy.

pinguxx avatar pinguxx commented on May 19, 2024

onconnected and ondisconnected

<element onconnected=${obj} >

from proppy.

fahad19 avatar fahad19 commented on May 19, 2024

may be something like this:

import { P } from './somewhere';

class MyComponent extends hyper.Component {
  get defaultState() {
    return {
      proppyProps: {},
    };
  }

  onconnected() {
    this.p = P();
    this._unsubscribe = this.p.subscribe(props => {
      this.state.proppyProps = props;
    });
  }

  ondisconnected() {
    this._unsubscribe();
  }

  render() {
    return this.html`
      <div onconnected=${this}>
        <p>Counter: ${this.state.proppyProps.counter}</p>
      </div>
    `;
  }
}

from proppy.

fahad19 avatar fahad19 commented on May 19, 2024

now imagine making a higher-order component, to make things easier even further:

function attach(ProppyFactory) {
  return function (Component) {
    // create and return a new Component with the logic
    // as explained in previous comment
  }
}

class MyPlainComponent extends hyper.Component {
  // ...
}

const MyAttachedComponent = attach(P)(MyPlainComponent);

from proppy.

pinguxx avatar pinguxx commented on May 19, 2024

subscribe still not working with my setup:
https://stackblitz.com/edit/basics-table-6-p-one?file=Table.js
I don't think this fixes the issue where the subscription stops working, unless you are saying a need a factory function and attach the same instance on every element....

from proppy.

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.