Build your first component
Create _components/click-counter.kscript inside your site folder.
<import package="KScript.Web" />
<type name="ClickCounter" extends="Component">
<field name="count" type="int" default="0" />
<fn name="render">
<return value="```html
<button type="button" @on-click(increment: $count)>Count is {$count}</button>
```" />
</fn>
</type>import KScript.Web
type ClickCounter extends Component {
count: int = 0
fn render() {
return ```html
<button type="button" @on-click(increment: $count)>Count is {$count}</button>
```
}
}Create index.html in the same site folder.
<h1>Counters</h1>
<click-counter />
<click-counter />Run kscript serve from the site folder and open the address it prints. Click the first button. It reads Count is 1, and the second still reads Count is 0.
The click handler makes each counter live. Each instance gets its own root and keeps its own count between clicks. Passing <click-counter count="{5}" /> starts an instance at five.
An explicit <live> block lets you choose the root element with as, defer rendering, or make only a portion of a component live. Use explicit blocks for interactive markup directly in a page. Plain display components render without loading the live runtime.