Skip to content
On this page

A counter component

Let's start with a classic, a counter component.

js
import { LitElement, html } from 'https://cdn.skypack.dev/lit-element'

class Counter extends LitElement {

  static properties = {
    counter: {type: Number},
  }

  constructor() {
    super()
    this.counter = 0
  }

  increment() {
    this.counter++
  }

  render() {
    return html`
      <div>${this.counter}</div>
      <button @click="${this.increment}">Increment</button>
    `
  }
}

customElements.define('qty-picker', Counter)
import { LitElement, html } from 'https://cdn.skypack.dev/lit-element'

class Counter extends LitElement {

  static properties = {
    counter: {type: Number},
  }

  constructor() {
    super()
    this.counter = 0
  }

  increment() {
    this.counter++
  }

  render() {
    return html`
      <div>${this.counter}</div>
      <button @click="${this.increment}">Increment</button>
    `
  }
}

customElements.define('qty-picker', Counter)

Using with HTML

html
<qty-picker counter="3"></qty-picker>
<qty-picker counter="3"></qty-picker>

Update props via JavaScript

js
const qtyPicker = document.querySelector('qty-picker')

qtyPicker.counter = 5
const qtyPicker = document.querySelector('qty-picker')

qtyPicker.counter = 5

Resources

Licenced MIT