A lightweight reactive primitive for creating auto-updating text nodes in DOM.
- Automatic DOM updates when values change
- Simple value binding syntax
- Support for multiple DOM locations through cloning
- Custom change handlers
import Reactive from 'html-tag-js/reactive';
const count = Reactive(0);
document.body.append(
<div>
<h1>{count}</h1>
<button onclick={() => count.value++}>Increment</button>
</div>
);
Creates a reactive text node
const message = Reactive('Hello');
Property | Description |
---|---|
value |
Get/set the current value (triggers DOM updates) |
onChange |
Callback function when value changes |
Method | Description |
---|---|
toString() |
Returns string representation of the value |
static isReactive(value) |
Check if a value is a reactive instance |
import Reactive from 'html-tag-js/reactive';
// Create reactive value
const temp = Reactive(25);
// Add change handler
temp.onChange = (value) => {
console.log('New temperature:', value);
};
// Create UI with JSX-like syntax
document.body.append(
<div>
<h2>Temperature Control</h2>
<div class="display">{temp}°C</div>
<button onclick={() => temp.value--}>Cooler</button>
<button onclick={() => temp.value++}>Warmer</button>
{/* Clone works in multiple locations */}
<div class="sidebar">Current: {temp}</div>
</div>
);
// All DOM instances update automatically
const counter = Reactive(0);
setInterval(() => counter.value++, 1000);
// Use curly braces in JSX templates
<div>Count: {counter}</div>
const mainDisplay = Reactive(100);
const sidebarDisplay = mainDisplay.clone();
// Updating either affects both DOM locations
mainDisplay.value = 200; // Updates both displays
const user = Reactive('Anonymous');
user.onChange = (name) => {
console.log(`User changed to: ${name}`);
};
- Use for primitive values (numbers, strings)
- Clean up event handlers when unmounting