1. Home
  2. /
  3. Hyperapp
  4. /
  5. Counter with Auto Increment

Counter with Auto Increment - Hyperapp Typing CST Test

Loading…

Counter with Auto Increment — Hyperapp Code

Counter that automatically increments every second.

import { h, app } from 'https://unpkg.com/hyperapp?module';
const state = { count: 0, isDark: false, auto: false };
const actions = {
	increment: () => s => ({ count: s.count + 1 }),
	decrement: () => s => ({ count: s.count - 1 }),
	reset: () => s => ({ count: 0 }),
	toggleTheme: () => s => ({ isDark: !s.isDark }),
	toggleAuto: () => s => {
		if (!s.auto) { s.timer = setInterval(() => actions.increment(), 1000); }
		else clearInterval(s.timer);
		return { auto: !s.auto };
	}
};
const view = (s, a) => (
	h('div', { class: s.isDark ? 'dark-theme' : 'light-theme' }, [
		h('h2', {}, 'Counter: ' + s.count),
		h('div', {}, [
		h('button', { onclick: a.increment }, '+'),
		h('button', { onclick: a.decrement }, '-'),
		h('button', { onclick: a.reset }, 'Reset'),
		h('button', { onclick: a.toggleAuto }, s.auto ? 'Stop Auto' : 'Start Auto')
		]),
		h('button', { onclick: a.toggleTheme }, 'Toggle Theme')
	])
);
app({ init: state, view, node: document.body, actions });

Hyperapp Language Guide

Hyperapp is an ultra-lightweight (≈1 KB), functional JavaScript library for building user interfaces using a minimalist architecture of state, actions, and a virtual DOM. It emphasizes simplicity, purity, and predictable UI updates.

Primary Use Cases

  • ▸Tiny SPAs or micro-frontends
  • ▸Browser extensions
  • ▸IoT dashboards
  • ▸Static sites with light interactivity
  • ▸Widgets or embeddable UI components

Notable Features

  • ▸1 KB bundle size
  • ▸Functional, Elm-like architecture
  • ▸Pure actions for state updates
  • ▸Minimal and efficient virtual DOM
  • ▸Built-in global state and view system

Origin & Creator

Created by Jorge Bucaran in 2017 as a tiny yet expressive alternative to React and Elm, designed to be minimal and functional.

Industrial Note

Hyperapp is specialized for teams that want extremely small bundles, functional UI patterns, and codebases with minimal abstraction - perfect for microtools, PWAs, and embedded UIs.

More Hyperapp Typing Exercises

Hyperapp Simple CounterHyperapp Counter with StepHyperapp Counter with Max/MinHyperapp Counter with Double IncrementHyperapp Counter with Even/Odd IndicatorHyperapp Counter with Max/Min and StepHyperapp Counter with LocalStorageHyperapp Counter with Color Themes

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher