Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any plan to use lit 2.0 controllers ? #7

Open
christophe-g opened this issue Apr 26, 2021 · 11 comments
Open

Any plan to use lit 2.0 controllers ? #7

christophe-g opened this issue Apr 26, 2021 · 11 comments

Comments

@christophe-g
Copy link
Contributor

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/

@gitaarik
Copy link
Owner

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

@christophe-g
Copy link
Contributor Author

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.

@tommillard
Copy link

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.

@christophe-g
Copy link
Contributor Author

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

@gitaarik
Copy link
Owner

gitaarik commented Aug 4, 2021

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

@justinfagnani
Copy link

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

@madeInLagny
Copy link

@justinfagnani
Hey Justin. I am struggling to develop state management with Lit reactive controller. Is there some (non TS) template that I can use to get started ?

@christophe-g
Copy link
Contributor Author

Hey @madeInLagny

Tu peux jeter un coup d'oeil at https://github.com/lit-apps/lit-app/tree/main/packages/state

Source is TS though.

@madeInLagny
Copy link

This is great and really what I am looking for. But I'd like to use a non TS version. Is anyone aware of an existing repo?

@christophe-g
Copy link
Contributor Author

christophe-g commented May 3, 2023 via email

@justinfagnani
Copy link

Controllers don't have anything to do with TypeScript. They're just objects that call .addController() on the host element, like the example I showed above, or the example here: https://lit.dev/docs/composition/controllers/ (just flip the JS/TS switch to JS).

Then you implement controller callbacks like hostUpdate().

Is there anything specific you're having problems with?

This discussion might be off-topic for this repo/thread though. Feel free to open something in https://github.com/lit/lit/discussions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants