Skip to content

Commit a55fe37

Browse files
committed
with/this/dispatch for counter
1 parent e644617 commit a55fe37

File tree

1 file changed

+18
-2
lines changed
  • typescript/packages/lookslike-high-level/src/recipes

1 file changed

+18
-2
lines changed

typescript/packages/lookslike-high-level/src/recipes/counter.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
import { h } from "@commontools/common-html";
2-
import { Spell, type OpaqueRef } from "@commontools/common-builder";
2+
import { Spell, type OpaqueRef, handler } from "@commontools/common-builder";
33

44
type CounterState = {
55
title: string;
66
count: number;
77
};
88

9+
const thisHandler = handler<
10+
{},
11+
{ count: number }
12+
>(function () {
13+
this.count += 1;
14+
});
15+
16+
const withHandler = handler<
17+
{},
18+
{ count: number }
19+
>(function ({}, state) {
20+
state.count += 1;
21+
});
22+
923
export class CounterSpell extends Spell<CounterState> {
1024
constructor() {
1125
super();
@@ -32,7 +46,9 @@ export class CounterSpell extends Spell<CounterState> {
3246
<div>
3347
<common-input value={title} oncommon-input={this.dispatch("title")} />
3448
<p>count: {count}</p>
35-
<common-button onclick={this.dispatch("increment")}>Increment</common-button>
49+
<common-button onclick={this.dispatch("increment")}>dispatch</common-button>
50+
<common-button onclick={thisHandler.bind({count})}>this</common-button>
51+
<common-button onclick={withHandler.with({count})}>with</common-button>
3652
</div>
3753
);
3854
}

0 commit comments

Comments
 (0)