-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch.go
71 lines (57 loc) · 990 Bytes
/
scratch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"strconv"
"github.com/speier/gowasm/pkg/server"
"github.com/speier/gowasm/pkg/vdom"
)
// user
type State struct {
Count int
}
func main() {
render(v1)
render(v2("bar"))
c := &TestComponent{1}
render(c.Render)
c.Inc(4)
}
func v1() *vdom.VNode {
return vdom.H("div", nil,
vdom.H("foo", nil),
)
}
func v2(s string) func() *vdom.VNode {
return func() *vdom.VNode {
return vdom.H("div", nil,
vdom.H(s, nil),
)
}
}
// pkg
type Context interface {
Update()
}
func render(view func() *vdom.VNode) {
str := server.RenderToString(view())
fmt.Println(str)
}
func renderComp(c Component) {
str := server.RenderToString(c.Render())
fmt.Println(str)
}
type Component interface {
Render() *vdom.VNode
}
type TestComponent struct {
Count int
}
func (c *TestComponent) Inc(by int) {
c.Count += by
render(c.Render)
}
func (c *TestComponent) Render() *vdom.VNode {
return vdom.H("div", nil,
vdom.H(strconv.Itoa(c.Count), nil),
)
}