Skip to content

Instantly share code, notes, and snippets.

@MatthiasKainer
Last active May 22, 2020 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthiasKainer/bb3f0af88381877bbf14ad9017acdb99 to your computer and use it in GitHub Desktop.
Save MatthiasKainer/bb3f0af88381877bbf14ad9017acdb99 to your computer and use it in GitHub Desktop.
@customElement("demo-element")
export class DemoElement extends LitElement {
state: DispatcherType<DemoElementState>;
constructor() {
super()
// register dispatcher in the constructor
this.state = useDispatcher<DemoElementState>(this, { actions: [] })
}
render() {
// get the current state for each rendering
const { actions } = this.state.getState()
// Change the state from multiple different actions using the publish function
const { publish } = this.state
return html` <div>
<trigger-button name="Button1"
@trigger="${(e: CustomEvent) => publish({ actions: [...actions, e.detail] })}"
></trigger-button>
<trigger-button name="Button2"
@trigger="${(e: CustomEvent) => publish({ actions: [...actions, e.detail] })}"
></trigger-button>
<input
type="checkbox"
@change="${({ target }: CheckableInputElement) => publish({ actions: [...actions, `Element checked: ${target.checked}`] })}"
/>
</div>
<action-list .actions="${actions}"></action-list>`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment