File tree Expand file tree Collapse file tree 2 files changed +16
-11
lines changed
playground/internal/react Expand file tree Collapse file tree 2 files changed +16
-11
lines changed Original file line number Diff line number Diff line change 4646
4747var (
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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments