Overview
The Counter example is a minimal app that demonstrates the fundamentals of GlyphUI:- Component-based architecture
- Local state management with
setState - Event handling with the
onproperty - Virtual DOM rendering with
h()andhFragment()
Live Demo
View the complete source code on GitHub
What You’ll Learn
Component Class
Extending the base Component class
State Management
Using setState to update component state
Event Handling
Binding click events to methods
Virtual DOM
Creating elements with h() function
Complete Code
Component Implementation
counter.js
Key Concepts
1. Component Constructor
The constructor initializes the component with an initial state:- First argument
{}is for props (empty in this case) - Second argument defines the initial state
2. State Updates
ThesetState() method triggers a re-render:
setState() merges the new state with the existing state and automatically triggers a re-render.3. Event Handling
Events are bound using theon property:
on object maps event names to handler functions.
4. Virtual DOM Creation
Elements are created using helper functions:h(tag, props, children)- Creates a virtual DOM elementhFragment(children)- Groups multiple elements without a wrapperhString(text)- Creates a text node
Running the Example
Next Steps
Todo App
Learn about global state management with stores
Components Guide
Deep dive into component architecture
State Management
Learn more about managing state
Event Handling
Master event handling patterns