MCPcopy Create free account
hub / github.com/wavetermdev/waveterm / UseState

Function UseState

pkg/vdom/vdom.go:317–333  ·  view source on GitHub ↗
(ctx context.Context, initialVal T)

Source from the content-addressed store, hash-verified

315}
316
317func UseState[T any](ctx context.Context, initialVal T) (T, func(T)) {
318 vc, hookVal := getHookFromCtx(ctx)
319 if !hookVal.Init {
320 hookVal.Init = true
321 hookVal.Val = initialVal
322 }
323 var rtnVal T
324 rtnVal, ok := hookVal.Val.(T)
325 if !ok {
326 panic("UseState hook value is not a state (possible out of order or conditional hooks)")
327 }
328 setVal := func(newVal T) {
329 hookVal.Val = newVal
330 vc.Root.AddRenderWork(vc.Comp.WaveId)
331 }
332 return rtnVal, setVal
333}
334
335func UseStateWithFn[T any](ctx context.Context, initialVal T) (T, func(T), func(func(T) T)) {
336 vc, hookVal := getHookFromCtx(ctx)

Callers 2

PageFunction · 0.85
ButtonFunction · 0.85

Calls 2

getHookFromCtxFunction · 0.85
AddRenderWorkMethod · 0.80

Tested by 2

PageFunction · 0.68
ButtonFunction · 0.68