Quickstart
This guide will walk you through building a working counter application with O!. You’ll learn the core concepts of components, state management, and rendering.What You’ll Build
By the end of this tutorial, you’ll have a fully functional counter with increment and decrement buttons:Tutorial Steps
Create an HTML file
Start by creating a new The
counter.html file:type="module" attribute enables ES module support in the browser.Import O! functions
Import the necessary functions from O!:If you installed via npm, use:What each function does:
h()- Creates virtual DOM nodes (like React.createElement)x- Tagged template for HTML-like syntaxrender()- Renders components to the DOMuseState()- Manages component state
Define the Counter component
Create a functional component that accepts props:O! components are just JavaScript functions that:
- Accept a props object as the first argument
- Return virtual DOM nodes
- Can use hooks like
useState
Add state with useState
Use the How useState works:
useState hook to manage the counter value:- Takes an initial value (
initialValue) - Returns an array:
[currentValue, setterFunction] - Calling
setValue()updates the state and triggers a re-render
Build the UI with template syntax
Use the Template syntax notes:
x` ` tagged template to create your component’s HTML:- Use
classNameinstead ofclass(like React) - Use
onclick(lowercase) for event handlers - Embed JavaScript expressions with
${...} - Attribute values can be functions, strings, or numbers
Complete Example
Here’s the full working code:Try It Out
- Save the code above as
counter.html - Open it in your browser
- Click the
+and-buttons to see the counter in action!
Key Concepts Learned
Functional Components
Functional Components
O! components are pure JavaScript functions that take props and return virtual DOM nodes. No classes or special syntax required.
State Management
State Management
The
useState hook provides local component state. When you call the setter function, O! automatically re-renders your component with the new state.Template Syntax
Template Syntax
The
x` ` tagged template lets you write HTML-like markup in your JavaScript. It’s converted to h() function calls under the hood.Virtual DOM
Virtual DOM
The
h() function creates virtual DOM nodes - lightweight JavaScript objects representing your UI. O! diffs these and updates the real DOM efficiently.Next Steps
- Explore the API Reference to learn about
useReduceranduseEffect - Check out more Examples to see O! in action
- Read about how O! works to understand the internals
Experiment Further
Try modifying the counter:- Add a reset button that sets the value back to
initialValue - Create multiple counters with different starting values
- Style your counter with inline styles:
<div style="color: blue"> - Add validation to prevent negative numbers