hooks

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 0 Imported by: 0

README

Hooks Package

Location: hooks/

The hooks package exposes the browser-facing hook API for GoWebComponents. It is a thin layer over the runtime hooks in internal/runtime/ and is built for js/wasm.

Available Hooks

UseState[T](initialValue T) (func() T, func(interface{}))

Local component state.

  • The getter returns the current value.
  • The setter accepts either a direct value or a functional update.
count, setCount := hooks.UseState(0)

setCount(1)
setCount(func(prev int) int { return prev + 1 })
UseEffect(effect func() func(), deps ...interface{})

Post-render side effects with optional cleanup.

hooks.UseEffect(func() func() {
    fmt.Println("mounted or deps changed")
    return func() {
        fmt.Println("cleanup")
    }
}, userID())
UseMemo(compute func() interface{}, deps ...interface{}) interface{}

Memoized computation.

filtered := hooks.UseMemo(func() interface{} {
    return expensiveFilter(items(), query())
}, items(), query()).([]Item)
UseCallback(fn interface{}, deps ...interface{}) interface{}

Stable callback identity across renders when dependencies do not change.

UseRef(initialValue interface{}) *runtime.RefValue

Mutable reference that survives renders without triggering rerenders.

UseId() string

Stable unique ID for accessibility wiring.

UseFetch(url string, options ...interface{}) (func() runtime.FetchState, func())

Hook-based fetch state and manual refetch() entrypoint.

GoUseFunc(fn interface{}) interface{}

Wraps Go functions for DOM event handlers.

Common supported shapes include:

  • func()
  • func(string)
  • func(js.Value)
  • func(dom.GoEvent)
  • error-returning equivalents for supported signatures

Hook Rules

  • Call hooks at the top level of a component.
  • Do not call hooks conditionally.
  • Keep hook call order stable between renders.

Examples

Documentation

Overview

Package hooks provides React-style hooks for managing component state and side effects in GoWebComponents.

This package implements the core hook functions that enable functional components to manage local state, perform side effects, memoize expensive computations, and optimize rendering performance.

Basic usage:

import "github.com/monstercameron/GoWebComponents/hooks"

func Counter(props dom.Attrs) *fiber.Element {
    count, setCount := hooks.UseState(0)

    return dom.Div(nil,
        dom.H1(nil, fmt.Sprintf("Count: %d", count())),
        dom.Button(map[string]interface{}{
            "onclick": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
                setCount(count() + 1)
                return nil
            }),
        }, "Increment"),
    )
}

Available hooks:

  • UseState[T]: Manages local component state with automatic re-rendering
  • UseEffect: Runs side effects with dependency tracking
  • UseMemo: Memoizes expensive computations
  • UseFunc: Creates stable function references (coming soon)

Hook Rules:

  1. Only call hooks at the top level of your component function
  2. Don't call hooks inside loops, conditions, or nested functions
  3. Always call hooks in the same order on every render

Violating these rules will cause runtime errors or unpredictable behavior.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL