(ctx context.Context)
| 299 | } |
| 300 | |
| 301 | func getHookFromCtx(ctx context.Context) (*VDomContextVal, *Hook) { |
| 302 | vc := getRenderContext(ctx) |
| 303 | if vc == nil { |
| 304 | panic("UseState must be called within a component (no context)") |
| 305 | } |
| 306 | if vc.Comp == nil { |
| 307 | panic("UseState must be called within a component (vc.Comp is nil)") |
| 308 | } |
| 309 | for len(vc.Comp.Hooks) <= vc.HookIdx { |
| 310 | vc.Comp.Hooks = append(vc.Comp.Hooks, &Hook{Idx: len(vc.Comp.Hooks)}) |
| 311 | } |
| 312 | hookVal := vc.Comp.Hooks[vc.HookIdx] |
| 313 | vc.HookIdx++ |
| 314 | return vc, hookVal |
| 315 | } |
| 316 | |
| 317 | func UseState[T any](ctx context.Context, initialVal T) (T, func(T)) { |
| 318 | vc, hookVal := getHookFromCtx(ctx) |
no test coverage detected