fahad19 / proppy

Functional props composition for UI components (React.js & Vue.js)
https://proppyjs.com
MIT License
939 stars 27 forks source link

hyperHTML: subscription not working #28

Open pinguxx opened 6 years ago

pinguxx commented 6 years ago

Using proppy directly, when i subscribe in my instance when the elements are updating the state the subscription its not called https://stackblitz.com/edit/basics-table-6-p-one?file=Table.js If i return the instance in the module (bad practice), it works https://stackblitz.com/edit/basics-table-6-p?file=Table.js

fahad19 commented 6 years ago

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:

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

pinguxx commented 6 years ago

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?

fahad19 commented 6 years ago

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.

pinguxx commented 6 years ago

onconnected and ondisconnected

<element onconnected=${obj} >
fahad19 commented 6 years ago

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>
    `;
  }
}
fahad19 commented 6 years ago

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);
pinguxx commented 6 years ago

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....