#1086·go-app

为什么基于输入封装的组件无法及时更新?

作者: jinguihua创建于 2025年8月8日更新于 2025年8月8日

//hello.go import ( "fmt" "GitHub.com/maxence-charriere/go-app/v10/pkg/app" time )

// Hello 类 type Hello struct { app.Compo name string }

// OnNav 方法 func (h *Hello) OnNav(ctx app.Context) { h.name = fmt.Sprintf("初始值%s", time.Now().Format("2006-01-02 15:04:05")) }

// Render 方法 func (h *Hello) Render() app.UI { return app.Div().Styles(map[string]string{ "display": "flex", "flex-direction": "column", }).Body( app.Input().Value(h.name).OnChange(h.ValueTo(&h.name)), NewMyInput(h.name, "测试01", &h.name), app.Button().Text("更新").OnClick(func(ctx app.Context, e app.Event) { h.name = "被更新" + fmt.Sprintf("%v", time.Now().UnixMilli()) ctx.Update() }), ) }

// MyInput 类 type MyInput struct { app.Compo value string placeholder string bindValue *string }

// NewMyInput 方法 func NewMyInput(value, placeValue string, bv *string) *MyInput { return &MyInput{ Compo: app.Compo{}, value: value, placeholder: placeValue, bindValue: bv, } }

// Render 方法 func (self *MyInput) Render() app.UI { return app.Div().Body( app.Input().Value(self.value).Placeholder(self.placeholder). OnChange(self.ValueTo(self.bindValue)), ) }

内容来源: maxence-charriere/go-app