Top Related Projects
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
:fire: An extremely fast, React-like JavaScript library for building modern user interfaces
Cybernetically enhanced web apps
A rugged, minimal framework for composing JavaScript behavior in your markup.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
Quick Overview
Hyperapp is a lightweight, functional, and reactive JavaScript library for building modern web applications. It provides a simple and minimalistic API for creating user interfaces, managing state, and handling user interactions.
Pros
- Lightweight: Hyperapp is a small library, weighing in at around 1.5KB gzipped, making it a great choice for performance-sensitive applications.
- Functional Programming: Hyperapp embraces a functional programming approach, which can lead to more maintainable and testable code.
- Virtual DOM: Hyperapp uses a virtual DOM implementation, which can improve performance by minimizing unnecessary DOM updates.
- Reactive Approach: Hyperapp's reactive nature makes it easy to manage state and update the UI in response to user actions.
Cons
- Limited Ecosystem: Compared to larger frameworks like React or Vue, Hyperapp has a smaller ecosystem of third-party libraries and tooling.
- Learning Curve: Hyperapp's minimalistic approach may require developers to learn new concepts, such as the Elm Architecture, which can be a barrier to entry for some.
- Lack of Widespread Adoption: Hyperapp is not as widely adopted as some of the more popular JavaScript frameworks, which may make it harder to find community support and resources.
- Limited Documentation: While the official documentation is generally good, some developers may find the lack of comprehensive documentation and examples a drawback.
Code Examples
Creating a Simple Counter App
import { app, h } from "hyperapp"
const state = {
count: 0
}
const actions = {
increment: state => ({ count: state.count + 1 }),
decrement: state => ({ count: state.count - 1 })
}
const view = (state, actions) => (
<div>
<h1>{state.count}</h1>
<button onclick={actions.increment}>+</button>
<button onclick={actions.decrement}>-</button>
</div>
)
app(state, actions, view, document.body)
Handling User Input
import { app, h } from "hyperapp"
const state = {
name: ""
}
const actions = {
updateName: (state, event) => ({ name: event.target.value })
}
const view = (state, actions) => (
<div>
<input type="text" oninput={actions.updateName} value={state.name} />
<p>Hello, {state.name}!</p>
</div>
)
app(state, actions, view, document.body)
Fetching Data from an API
import { app, h } from "hyperapp"
const state = {
loading: true,
data: null
}
const actions = {
fetchData: () => (state, actions) => {
fetch("https://api.example.com/data")
.then(response => response.json())
.then(data => actions.setData(data))
.catch(error => actions.setError(error))
},
setData: data => ({ loading: false, data }),
setError: error => ({ loading: false, error })
}
const view = (state, actions) => (
<div>
{state.loading ? (
<p>Loading...</p>
) : state.error ? (
<p>Error: {state.error.message}</p>
) : (
<pre>{JSON.stringify(state.data, null, 2)}</pre>
)}
<button onclick={actions.fetchData}>Fetch Data</button>
</div>
)
app(state, actions, view, document.body)
Getting Started
To get started with Hyperapp, you can follow these steps:
- Install Hyperapp using npm or yarn:
npm install hyperapp
- Create a new file (e.g.,
app.js
) and import the necessary functions from Hyperapp:
import { app, h } from "hyperapp"
- Define your application's state, actions, and view:
const state = {
Competitor Comparisons
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Pros of Preact
- Preact is a lightweight alternative to React, weighing in at just 3kB gzipped, making it a great choice for performance-sensitive applications.
- Preact has a large and active community, with a wide range of third-party libraries and tools available.
- Preact is highly compatible with React, allowing developers to easily migrate existing React projects to Preact.
Cons of Preact
- Preact has a smaller feature set compared to React, which may not be suitable for more complex applications.
- Preact's ecosystem is not as mature as React's, with fewer third-party libraries and tools available.
- Preact may not have the same level of enterprise-level support and resources as React.
Code Comparison
Hyperapp:
const state = {
count: 0
}
const actions = {
increment: state => ({ count: state.count + 1 }),
decrement: state => ({ count: state.count - 1 })
}
const view = (state, actions) => (
<div>
<h1>{state.count}</h1>
<button onclick={actions.increment}>+</button>
<button onclick={actions.decrement}>-</button>
</div>
)
Hyperapp.app(state, actions, view, document.body)
Preact:
const state = {
count: 0
}
const actions = {
increment: state => ({ count: state.count + 1 }),
decrement: state => ({ count: state.count - 1 })
}
const view = (state, actions) => (
<div>
<h1>{state.count}</h1>
<button onClick={actions.increment}>+</button>
<button onClick={actions.decrement}>-</button>
</div>
)
render(view(state, actions), document.body)
:fire: An extremely fast, React-like JavaScript library for building modern user interfaces
Pros of Inferno
- Inferno is known for its fast performance, making it a great choice for building high-performance web applications.
- Inferno has a large and active community, with a wide range of third-party libraries and tools available.
- Inferno's virtual DOM implementation is highly optimized, which can lead to faster rendering and better overall performance.
Cons of Inferno
- Inferno has a steeper learning curve compared to Hyperapp, as it has a more complex API and a larger set of features.
- Inferno's ecosystem is not as mature as Hyperapp's, with fewer third-party libraries and tools available.
- Inferno may have a higher overhead in terms of file size and bundle size, which can be a concern for some projects.
Code Comparison
Hyperapp:
const state = {
count: 0
}
const actions = {
increment: () => state => ({ count: state.count + 1 })
}
const view = (state, actions) => (
<div>
<h1>{state.count}</h1>
<button onclick={actions.increment}>Increment</button>
</div>
)
app(state, actions, view, document.body)
Inferno:
const state = {
count: 0
}
const actions = {
increment: () => (state) => ({ count: state.count + 1 })
}
const view = (state, actions) => (
<div>
<h1>{state.count}</h1>
<button onClick={actions.increment}>Increment</button>
</div>
)
Inferno.render(view(state, actions), document.body)
Cybernetically enhanced web apps
Pros of Svelte
- Reactive Updates: Svelte automatically updates the DOM when the state changes, reducing the need for manual DOM manipulation.
- Compiler-based Approach: Svelte compiles your components into highly-optimized JavaScript code, resulting in small bundle sizes and fast performance.
- Simplicity and Readability: Svelte's syntax is straightforward and easy to understand, making it a great choice for developers new to the framework.
Cons of Svelte
- Ecosystem Maturity: Svelte has a smaller ecosystem compared to more established frameworks like React and Vue, with fewer third-party libraries and tooling available.
- Learning Curve: While Svelte is relatively simple, developers coming from other frameworks may need to invest time in learning its unique approach and syntax.
- Server-side Rendering: Svelte's server-side rendering support is not as robust as some other frameworks, which may be a concern for certain use cases.
Code Comparison
Hyperapp (5 lines):
const state = { count: 0 }
const actions = {
increment: (state) => ({ count: state.count + 1 }),
decrement: (state) => ({ count: state.count - 1 })
}
Svelte (5 lines):
<script>
let count = 0;
function increment() { count += 1; }
function decrement() { count -= 1; }
</script>
A rugged, minimal framework for composing JavaScript behavior in your markup.
Pros of Alpine.js
- Simplicity: Alpine.js is known for its simplicity and ease of use, making it a great choice for developers who want to add interactivity to their web applications without the overhead of a full-fledged framework.
- Lightweight: Alpine.js is a very lightweight library, weighing in at just a few kilobytes, which can be beneficial for performance-sensitive applications.
- Familiar Syntax: Alpine.js uses a familiar syntax that is similar to Vue.js, which can make it easier for developers who are already familiar with that ecosystem.
Cons of Alpine.js
- Limited Ecosystem: Compared to Hyperapp, Alpine.js has a smaller ecosystem of plugins and third-party libraries, which can limit the available functionality and features.
- Lack of Routing: Alpine.js does not have built-in routing capabilities, which means developers may need to use a separate library or implement their own routing solution.
- Limited State Management: Alpine.js has a more basic approach to state management compared to Hyperapp, which may not be suitable for more complex applications.
Code Comparison
Hyperapp:
const state = {
count: 0
}
const actions = {
increment: (state) => ({ count: state.count + 1 }),
decrement: (state) => ({ count: state.count - 1 })
}
app({
state,
actions,
view: (state, actions) => (
<div>
<h1>{state.count}</h1>
<button onclick={actions.increment}>+</button>
<button onclick={actions.decrement}>-</button>
</div>
)
})
Alpine.js:
<div x-data="{ count: 0 }">
<h1 x-text="count"></h1>
<button x-on:click="count++">+</button>
<button x-on:click="count--">-</button>
</div>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
Pros of Solid
- Solid is known for its impressive performance, with a focus on efficient rendering and minimal overhead.
- The library's reactive system is highly optimized, leading to fast and responsive user interfaces.
- Solid's approach to state management is straightforward and easy to understand, making it a good choice for developers who prefer a more declarative style.
Cons of Solid
- Solid has a smaller community compared to Hyperapp, which may mean fewer available resources and third-party libraries.
- The learning curve for Solid can be steeper than Hyperapp, especially for developers who are more familiar with React or other popular frameworks.
- Solid's focus on performance and efficiency may come at the cost of some flexibility, as the library has a more opinionated approach to certain aspects of web development.
Code Comparison
Hyperapp:
const state = {
count: 0
}
const actions = {
increment: () => state => ({ count: state.count + 1 })
}
const view = (state, actions) => (
<div>
<h1>{state.count}</h1>
<button onclick={actions.increment}>+</button>
</div>
)
app(state, actions, view, document.body)
Solid:
import { createSignal } from 'solid-js'
function App() {
const [count, setCount] = createSignal(0)
return (
<div>
<h1>{count()}</h1>
<button onclick={() => setCount(count() + 1)}>+</button>
</div>
)
}
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Hyperapp
The tiny framework for building hypertext applications.
- Do more with lessâWe have minimized the concepts you need to learn to get stuff done. Views, actions, effects, and subscriptions are all pretty easy to get to grips with and work together seamlessly.
- Write what, not howâWith a declarative API that's easy to read and fun to write, Hyperapp is the best way to build purely functional, feature-rich, browser-based apps using idiomatic JavaScript.
- Smaller than a faviconâ1 kB, give or take. Hyperapp is an ultra-lightweight Virtual DOM, highly-optimized diff algorithm, and state management library obsessed with minimalism.
Here's the first example to get you started. Try it hereâno build step required!
<script type="module">
import { h, text, app } from "https://unpkg.com/hyperapp"
const AddTodo = (state) => ({
...state,
value: "",
todos: state.todos.concat(state.value),
})
const NewValue = (state, event) => ({
...state,
value: event.target.value,
})
app({
init: { todos: [], value: "" },
view: ({ todos, value }) =>
h("main", {}, [
h("h1", {}, text("To do list")),
h("input", { type: "text", oninput: NewValue, value }),
h("ul", {},
todos.map((todo) => h("li", {}, text(todo)))
),
h("button", { onclick: AddTodo }, text("New!")),
]),
node: document.getElementById("app"),
})
</script>
<main id="app"></main>
The app starts by setting the initial state and rendering the view on the page. User input flows into actions, whose function is to update the state, causing Hyperapp to re-render the view.
When describing how a page looks in Hyperapp, we don't write markup. Instead, we use h()
and text()
to create a lightweight representation of the DOM (or virtual DOM for short), and Hyperapp takes care of updating the real DOM efficiently.
Installation
npm install hyperapp
Documentation
Learn the basics in the Tutorial, check out the Examples, or visit the Reference.
Packages
Official packages provide access to Web Platform APIs in a way that makes sense for Hyperapp. For third-party packages and real-world examples, browse the Hyperawesome collection.
Package | Status | About |
---|---|---|
@hyperapp/dom | Inspect the DOM, focus and blur. | |
@hyperapp/svg | Draw SVG with plain functions. | |
@hyperapp/html | Write HTML with plain functions. | |
@hyperapp/time | Subscribe to intervals, get the time now. | |
@hyperapp/events | Subscribe to mouse, keyboard, window, and frame events. | |
@hyperapp/http | Talk to servers, make HTTP requests (#1027). | |
@hyperapp/random | Declarative random numbers and values. | |
@hyperapp/navigation | Subscribe and manage the browser URL history. |
Need to create your own effects and subscriptions? You can do that too.
Help, I'm stuck!
If you've hit a stumbling block, hop on our Discord server to get help, and if you remain stuck, please file an issue, and we'll help you figure it out.
Contributing
Hyperapp is free and open-source software. If you want to support Hyperapp, becoming a contributor or sponsoring is the best way to give back. Thank you to everyone who already contributed to Hyperapp! <3
License
Top Related Projects
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
:fire: An extremely fast, React-like JavaScript library for building modern user interfaces
Cybernetically enhanced web apps
A rugged, minimal framework for composing JavaScript behavior in your markup.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot