gitaarik / lit-state

Simple shared component state management for LitElement.
https://gitaarik.github.io/lit-state/build/
GNU Lesser General Public License v3.0
139 stars 11 forks source link

State var change callback #10

Closed madeInLagny closed 2 years ago

madeInLagny commented 2 years ago

Hello, Thank you for your great work with lit-state. I am trying to catch a state var change call back to trigger some actions when a specific state var gets updated.

updated(changedProperties) {
  if (changedProperties.has('myState.collapsed')) {
doSomething()
  }
}

won't work because myState.collapsed is not a component property. What is the recommended method to catch that state var update callback ?

christophe-g commented 2 years ago

I would add an observer on connectedCallback in this case:

connectedCallback() {
  super.connectedCallback()
  myState.addObserver(() => {dothings}, ['collapsed'])
}

and remove the observer at disconnectedCallback

madeInLagny commented 2 years ago

Will do so. Thanks

gitaarik commented 2 years ago

That's the way indeed. Also documented here.