lit-state icon indicating copy to clipboard operation
lit-state copied to clipboard

Any plan to use lit 2.0 controllers ?

Open christophe-g opened this issue 3 years ago • 11 comments

Hey @gitaarik

Just curious to know if there are plans to upgrade to lit 2.0 now that it is on release candidate stage?

Apparently controllers would be perfectly fitted to lit-state : https://lit.dev/docs/composition/controllers/

christophe-g avatar Apr 26 '21 13:04 christophe-g

Hi @christophe-g, sorry I haven't replied to this yet. I haven't looked into this new technology of Lit, and currently don't have much time for it either. I would like to look into it when I have more time, so I'll keep you updated about it. Thanks for the suggestion anyway :).

gitaarik avatar Jul 29 '21 10:07 gitaarik

No problem,

currently don't have much time for it either

I so understand that!

I haven't played with controllers yet, but from what I got, they could (possibly greatly) simplify how lit-state checks state changes and re-trigger a requestUpdate.

christophe-g avatar Jul 29 '21 11:07 christophe-g

I agree with the above, but having played with controllers I’m still using lit-state.

The biggest difference I see between them is that it seems like a controller is almost just an extension of the lit element. Or to put it another way, you have 1 controller for 1 element.

The great advantage of lit-state is that multiple elements can hook into a single state object.

tommillard avatar Jul 29 '21 12:07 tommillard

The great advantage of lit-state is that multiple elements can hook into a single state object.

Hmm, I think the idea would be to use the controller(s) as an interface between ONE state object and multiple elements. If that is not possible, forget about my suggestion ; )

christophe-g avatar Jul 29 '21 13:07 christophe-g

Yes, I guess your suggestion would be to use Lit controllers inside LitState?

gitaarik avatar Aug 04 '21 09:08 gitaarik

The idea would be to use a controller instead of a mixin to connect a component to a store:

Translating from the readme example, you could do this:

import {LitElement, html} from 'lit-element';
import {StateController} from 'lit-element-state';
import {myState} from './my-state.js';

class MyComponent extends LitElement {
  private _state = new StateController(this);

  render() {
    return html`
      <h1>Counter: ${myState.counter}</h1>
      <button @click=${() => myState.counter++}></button>
    `;
  }

}

but perhaps it would be nice to have a direct connection between a component and the state it uses so you could also pass the state to the controller:

class MyComponent extends LitElement {
  private _state = new StateController(this, [myState]);
}

justinfagnani avatar Jan 17 '22 22:01 justinfagnani