Counter Example
This example demonstrates the core concepts of O!: state management withuseState, template syntax with x, and event handling.
Complete Counter Component
Key Concepts
State Management with useState
The useState hook provides local component state that persists between updates:
value- The current state valuesetValue- Function to update the state and trigger a re-renderinitialValue- The initial state value (can be from props)
Template Syntax with x
The x`...` template tag converts HTML-like syntax into virtual nodes:
x templates:
- Use
classNameinstead ofclass(DOM property names) - Constant attribute values must be double-quoted:
className="counter" - Placeholders can be used for:
- Tag names:
<${MyComponent}> - Attribute values:
onclick=${handler} - Text content:
<div>${value}</div>
- Tag names:
- Tags can be self-closing:
<img src="..." />
Event Handlers
Event handlers are attached as DOM properties:- Use lowercase event names:
onclick,onchange,onsubmit - Pass functions as placeholders (not strings)
- Handlers can call state setters to trigger updates
Rendering
Therender function mounts the component to the DOM:
- First argument: Virtual node created with
h(Component, props) - Second argument: DOM container element
- The component tree is rendered and patched into the container