Counter Example
Level: Beginner
Concepts: Basic state management, event handling
What This Example Demonstrates
- Using
hooks.UseState for number state
- Button click handlers with
js.FuncOf
- Simple increment/decrement operations
- State updates triggering re-renders
- Basic Tailwind CSS styling
Key Code
count, setCount := hooks.UseState(0)
currentCount := count()
increment := func(this js.Value, args []js.Value) interface{} {
setCount(currentCount + 1)
return nil
}
Learning Objectives
- Understand how to create and update state in GoWebComponents
- Learn to attach event handlers to DOM elements
- See how state changes trigger component re-renders
- Build interactive UI with functional programming patterns
Running
cd examples
./build.ps1 -Example 01-counter
Then open 01-counter/counter.html in a web server.