Skip to content

Commit dacc14d

Browse files
Making react work
1 parent 32fb299 commit dacc14d

2 files changed

Lines changed: 54 additions & 51 deletions

File tree

playground/internal/react/react.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ type (
3030

3131
StateMap map[string]any
3232

33+
State[T any] struct{ getter, setter *js.Object }
34+
3335
// Component is a React component used when rendering with state.
3436
// See: https://react.dev/reference/react/Component
3537
Component struct{ *js.Object }
36-
37-
State[T any] struct{ getter, setter *js.Object }
3838
)
3939

4040
func reactDom() *js.Object { return js.Global.Get(`ReactDOM`) }
@@ -66,30 +66,6 @@ func checkNode(n Node) Node {
6666
}
6767
}
6868

69-
func NewComponent(props Props, init StateMap, render func(*Component) Node) *Element {
70-
reactComp := react().Get("Component")
71-
ctor := js.MakeFunc(func(this *js.Object, args []*js.Object) any {
72-
reactComp.Call(`call`, props)
73-
this.Set(`state`, init)
74-
return nil
75-
})
76-
rf := js.MakeFunc(func(this *js.Object, args []*js.Object) any {
77-
return render(&Component{Object: this})
78-
})
79-
ctor.Set(`prototype`, reactComp.Get(`prototype`))
80-
ctor.Get(`prototype`).Set(`constructor`, ctor)
81-
ctor.Get("prototype").Set("render", rf)
82-
return &Element{Object: react().Call(`createElement`, ctor)}
83-
}
84-
85-
func (c *Component) GetState(key string) *js.Object {
86-
return c.Get("state").Get(key)
87-
}
88-
89-
func (c *Component) SetState(newState StateMap) {
90-
c.Call("setState", newState)
91-
}
92-
9369
func StrictMode(children ...Node) *Element {
9470
return CreateElement(react().Get(`StrictMode`), nil, children...)
9571
}
@@ -124,3 +100,30 @@ func UseState[T any](initial T) State[T] {
124100
func (s State[T]) Get() T { return s.getter.Interface().(T) }
125101

126102
func (s State[T]) Set(v T) { s.setter.Invoke(v) }
103+
104+
func NewComponent(props Props, init StateMap, render func(*Component) Node) *Element {
105+
reactComp := react().Get("Component")
106+
ctor := js.MakeFunc(func(this *js.Object, args []*js.Object) any {
107+
reactComp.Call(`call`, props)
108+
this.Set(`state`, init)
109+
//if constructor != nil {
110+
// js.Global.Get(`Reflect`).Call(`apply`, constructor, this, []any{})
111+
//}
112+
return nil
113+
})
114+
rf := js.MakeFunc(func(this *js.Object, args []*js.Object) any {
115+
return render(&Component{Object: this})
116+
})
117+
ctor.Set(`prototype`, reactComp.Get(`prototype`))
118+
ctor.Get(`prototype`).Set(`constructor`, ctor)
119+
ctor.Get("prototype").Set("render", rf)
120+
return &Element{Object: react().Call(`createElement`, ctor)}
121+
}
122+
123+
func (c *Component) GetState(key string) *js.Object {
124+
return c.Get("state").Get(key)
125+
}
126+
127+
func (c *Component) SetState(newState StateMap) {
128+
c.Call("setState", newState)
129+
}

playground/playground.js

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)