Skip to content

Commit ed5c4a7

Browse files
Reworking react
1 parent 9552629 commit ed5c4a7

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

playground/internal/react/bindings.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ type (
4646

4747
var (
4848
ErrUndefinedPropKey = errors.New(`react: undefined prop key`)
49-
ErrPropNotFunc = errors.New(`react: prop is not a function`)
5049
ErrRefNotInitialized = errors.New(`react: Ref not initialized`)
5150
)
5251

@@ -72,14 +71,20 @@ func (p Props) Affirm() Props {
7271
return p
7372
}
7473

74+
// As retrieves the property with the given name from the Props
75+
// and converts it to the specified type T.
76+
func As[T any](props Props, name string) T {
77+
if prop, ok := props[name]; ok {
78+
return prop.(T)
79+
}
80+
panic(ErrUndefinedPropKey)
81+
}
82+
7583
// AsFunc retrieves the property with the given name from the Props
76-
// and asserts that it is a function of type Func.
77-
func (p Props) AsFunc(name string) Func {
78-
if prop, ok := p[name]; ok {
79-
if fn, ok := prop.(func(...any) *js.Object); ok {
80-
return Func(fn)
81-
}
82-
panic(ErrPropNotFunc)
84+
// and converts it to a function of type Func.
85+
func AsFunc(props Props, name string) Func {
86+
if prop, ok := props[name]; ok {
87+
return Func(prop.(func(...any) *js.Object))
8388
}
8489
panic(ErrUndefinedPropKey)
8590
}

playground/internal/react/codeBox.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ func CodeBox(code string, setCode func(string)) *Element {
1313
})
1414
}
1515

16-
func codeBoxComponent(prop Props) *Element {
16+
func codeBoxComponent(props Props) *Element {
1717
cba := &codeBoxAssistant{
18-
code: prop[`code`].(string),
19-
setCode: prop.AsFunc(`setCode`),
18+
code: As[string](props, `code`),
19+
setCode: AsFunc(props, `setCode`),
2020
textAreaRef: UseRef(),
2121
}
2222

0 commit comments

Comments
 (0)