Skip to content

Commit ef690fb

Browse files
Making react work
1 parent dacc14d commit ef690fb

3 files changed

Lines changed: 4140 additions & 20106 deletions

File tree

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
package react
22

3-
import "github.com/gopherjs/gopherjs/js"
3+
import (
4+
"time"
5+
6+
"github.com/gopherjs/gopherjs/js"
7+
)
48

59
func NewCounter(initName string, initAge int) *Element {
6-
return NewComponent(Props{}, map[string]any{
7-
`name`: initName,
8-
`age`: initAge,
9-
}, func(c *Component) Node {
10+
var name *State[string]
11+
var age *State[float64]
12+
13+
gnCounter := func(props Props) *Element {
14+
name = UseState(initName)
15+
age = UseState(float64(initAge))
16+
1017
return StrictMode(
1118
CreateElement(`input`, Props{
12-
`value`: c.GetState(`name`).String(),
19+
`value`: name.Get(),
1320
`onChange`: func(e *js.Object) {
14-
c.SetState(map[string]any{
15-
`name`: e.Get(`target`).Get(`value`).String(),
16-
})
21+
name.Set(e.Get(`target`).Get(`value`).String())
1722
},
1823
}),
1924
Button(`Increment age`, nil, func() {
20-
c.SetState(map[string]any{
21-
`age`: c.GetState(`age`).Int() + 1,
22-
})
25+
age.Set(age.Get() + 1)
2326
}),
2427
CreateElement(`p`, nil,
25-
`Hello, `, c.GetState(`name`).String(), `. You are `, c.GetState(`age`).Int(), `.`),
28+
`Hello, `, name.Get(), `. You are `, age.Get(), `.`),
2629
)
27-
})
30+
}
31+
32+
go func() {
33+
t := time.NewTicker(2 * time.Second)
34+
for range t.C {
35+
age.Set(age.Get() + 1)
36+
}
37+
}()
38+
39+
return CreateElement(gnCounter, nil)
2840
}

playground/internal/react/react.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package react
22

33
import (
4-
"fmt"
5-
64
"github.com/gopherjs/gopherjs/js"
75
)
86

@@ -58,12 +56,13 @@ func CreateElement(typ any, props Props, children ...Node) *Element {
5856
}
5957

6058
func checkNode(n Node) Node {
61-
switch c := n.(type) {
62-
case *Element, string, int, float64:
63-
return c
64-
default:
65-
panic(fmt.Errorf(`unsupported child type %T`, c))
66-
}
59+
//switch c := n.(type) {
60+
//case *Element, string, int, float64:
61+
// return c
62+
//default:
63+
// panic(fmt.Errorf(`unsupported child type %T`, c))
64+
//}
65+
return n
6766
}
6867

6968
func StrictMode(children ...Node) *Element {
@@ -92,14 +91,24 @@ func Button(value string, props Props, onClick func()) *Element {
9291
return CreateElement(`input`, props)
9392
}
9493

95-
func UseState[T any](initial T) State[T] {
94+
func UseState[T string | float64](initial T) *State[T] {
9695
s := react().Call(`useState`, initial)
97-
return State[T]{getter: s.Index(0), setter: s.Index(1)}
96+
return &State[T]{getter: s.Index(0), setter: s.Index(1)}
9897
}
9998

100-
func (s State[T]) Get() T { return s.getter.Interface().(T) }
99+
func (s *State[T]) Get() T {
100+
if s == nil || s.getter == nil {
101+
var zero T
102+
return zero
103+
}
104+
return s.getter.Interface().(T)
105+
}
101106

102-
func (s State[T]) Set(v T) { s.setter.Invoke(v) }
107+
func (s *State[T]) Set(v T) {
108+
if s != nil && s.setter != nil {
109+
s.setter.Invoke(v)
110+
}
111+
}
103112

104113
func NewComponent(props Props, init StateMap, render func(*Component) Node) *Element {
105114
reactComp := react().Get("Component")

0 commit comments

Comments
 (0)