• Stars
    star
    249
  • Rank 162,987 (Top 4 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tiny and simple React clone

O!

Build Status npm gzip size

O!

A very small (<1KB) library to explain how React-like libraries work. Never meant to be used in any of the serious projects. But, hey, it has a JSX-like language and even Hooks! How cool is that? It started as a quick morning experiment some cold autumn Friday. They joy of seeing a simple counter component finally working made it warmer. It's really an exercise in minimalism, and nothing more.


Features

  • Something around 1KB when minified and gzipped.
  • Has h() and render() API with the support of functional components.
  • Has useState, useReducer and useEffect hooks, just like real React.
  • Has a custom template syntax sugar, very similar to normal HTML.
  • Supports SVG tags.
  • Zero dependencies, obviously.
  • No transpiler needed. Just import a single file as a module and start writing code.
  • Should be not so hard to understand and then move on to some more appropriate React clones.

Example to whet your appetite

import { h, x, render, useState } from 'o.mjs';

const Counter = ({ initialValue = 0 }) => {
  const [value, setValue] = useState(initialValue);
  return x`
    <div className="counter">
      <div>${value}</div>
      <div className="row">
        <button onclick=${() => setValue(value + 1)}>+</button>
        <button onclick=${() => setValue(value - 1)}>-</button>
      </div>
    </div>
  `;
};

render(h(Counter, { initialValue: 10 }), document.body);

See it live

API

Hey, it might be quicker to read the sources than this text. Anyway,

  • render(component, containerNode) - renders component into containerNode. In other words, it patches the containerNode to reflect the component virtual node. Beware, diffing algorithm is very dumb and inefficient. Does not return anything.
  • h(nodeName|Function, properties, ...children) - returns a virtual node. nodeName can be a HTML tag name (string) to a functional component (JS function). Properties must be an object. Property names closely reflect DOM Node properties, so use className instead of class, also style is a CSS string, not an object. The only artificial property is k which is a key used to keep component state between the updates if the position of the component in the DOM tree has changed. Finally, children is a variadic argument containing other h() nodes or raw JS strings if you want a text node.
  • x`<div>...</div>` - syntax sugar to replace multiple h() calls. Accepts regular HTML code and converts it into virtual node tree. Template placeholders can only be used as tag names, attribute value or text between the tags. Constant attribute values must be double-quoted (even if they are numbers). Tags can be self-closing. Only one top-level tag is allowed. And yes, there must be at least one top-level tag.
  • useState(initialValue) - returns an array [value, setValue] of the current state and a setter for the state, that would also trigger an update for the current component.
  • useReducer(reducer, initialValue) - returns an array [state, dispatch] of the current state and a function to dispatch an action to the reducer. reducer is a (state, action) => newState function that returns a new state based on the current state and the given action. Use this as an alternative to useState is you need a more complex logic in your states.
  • useEffect(callback, deps) - tries to fire a callback when component is rendered, but only if the deps array has changed since the last call. An efficient way to add side effects to your functional components.

Finally, what is a functional component? In our case it is a regular JS function that takes two arguments - an object of component properties and a forceUpdate callback, that will trigger component update once called. Functional components must return a tree of virtual nodes constructed with h() calls or x` ` syntax.

What a weird name for a project

The library is called "O!". It's a sound of realisation, once you understood how simple it is. Or despair, if you caught a fatal bug after you decided to use this in production. It also resembles a zero, which is a metaphor for both, library footprint and usefulness. More details on how this library originated at https://zserge.com/posts/worst-react-ever/

License

Code is distributed under Apache 2.0 license, feel free to use it in your proprietary projects as well.

More Repositories

1

lorca

Build cross-platform modern desktop apps in Go + HTML5
Go
7,954
star
2

jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
C
3,633
star
3

awfice

The world smallest office suite
HTML
3,462
star
4

fenster

The most minimal cross-platform GUI library
C++
513
star
5

tray

Cross-platform, super tiny C99 implementation of a system tray icon with a popup menu.
C
484
star
6

partcl

ParTcl - a micro Tcl implementation
C
467
star
7

metric

Minimal metrics for Go (counter/gauge/histogram). No dependencies. Compatible with expvar. Web UI included.
Go
353
star
8

luash

Tiny lua module to write shell scripts with lua (inspired by Python's sh module)
Lua
302
star
9

pt

Protothreads (coroutines) in C99. Highly portable, but work best in low-end embedded systems.
C
267
star
10

lua-promises

A+ promises in Lua
Lua
216
star
11

log

Ultimately minimal (yet very convenient) logger for Android and Java
Java
157
star
12

tojvm

A toy JVM in Go
Go
156
star
13

webview-python

Python bindings to webview
Objective-C
151
star
14

bfapi

Resilient, scalable Brainf*ck, in the spirit of modern systems design
Go
144
star
15

nokia-composer

Nokia Composer in 512 bytes
HTML
125
star
16

expr

Fast and lightweight math expression evaluator in C99
C
119
star
17

hid

Simple HID driver for Go (pure golang, no dependencies, no cgo)
Go
119
star
18

zs

Absolutely minimal static site generator in Go (powers https://zserge.com)
Go
91
star
19

tinysh

Tiny UNIX shell, de-obfuscated, modernized, and "rewritten in Rust".
C
88
star
20

nanonn

A nano-framework for neural networks
Rust
83
star
21

dotfiles

git clone --bare https://github.com/zserge/dotfiles $HOME/.dotfiles && git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME checkout
Vim Script
79
star
22

lc3-forth

Post-Apocalyptic Computing: bootstrapping Forth environment for LC-3 CPU
C
79
star
23

1bitr

Minimalistic text-based 1-bit music tracker
C
73
star
24

carnatus

A tiny chess engine in Go (sunfish port)
Go
65
star
25

odetoj

Rewrite of Arthur Whitney's one-page J interpreter in Rust
Rust
61
star
26

headline

Ascetic RSS reader in JavaScript, no server required
JavaScript
57
star
27

glob-grep

A little experiment: compare the languages aimed to replace C
Zig
52
star
28

buckbone

A simple android project generator for the Buck build system
Shell
52
star
29

q

Tiny and simple VueJS clone
JavaScript
46
star
30

beep

Cross-platform beep() function
C
43
star
31

slide

An attempt to implement Trikita Slide for desktop
C++
39
star
32

figma-simplify-path

Figma plugin to simplify vector paths
JavaScript
26
star
33

mucks

A tiny terminal session manager for Tmux, Screen and DVTM
Shell
20
star
34

anvil-kotlin-demos

Minimal tutorial/demos for Anvil+Kotlin
Kotlin
18
star
35

zserge.github.io

My static site
HTML
14
star
36

kv

An ultimately minimal persistent key-value store + LRU cache
Go
12
star
37

jsmn.lua

The world fastest JSON parser ported to Lua
Lua
11
star
38

aint

Code for the "AI or AIN'T" blog posts
Go
11
star
39

yu

Yu is a tee-like tool, but with rotation feature like logrotate
C
10
star
40

tab

🎼 A tiny CLI tool to render tabs for music instruments (🎹🎷🎺🎸🪕🪈 and many others!)
C
9
star
41

bsoz

One of the most minimal MOS6502 and retro computer emulators!
C
8
star
42

mdns

Very pragmatic mDNS implementation in Go
Go
8
star
43

kveer

A tiny in-memory key-value storage in Go with optional persistence (atomic backup file, or append-only)
Go
7
star
44

bf

Well, everyone has to write a brainf*ck interpreter at some point
C
7
star
45

covered

Trello Cover Card Generator
JavaScript
6
star
46

toy-java-agent

Toy Java agent
Java
6
star
47

atomicwriter

Atomic file writes in Go (using a unique temporary file and atomic rename)
Go
5
star
48

lex

A library for writing lexers in Go
Go
4
star
49

textizer

Minimal android widgets in Scheme
Java
4
star
50

chess

JavaScript
4
star
51

ping

An ultimately minimal social network, messaging, pub/sub and home automation app
4
star
52

tinylangs

Real programming langauges in 50 lines of code
Python
4
star
53

photo

Minimalistic private photo booth
HTML
3
star
54

incr

incr.it backend
JavaScript
3
star
55

zine

Tiny CSS template to produce micro-zines (folded 8-page magazines)
Python
3
star
56

one-click-hugo-cms

CSS
2
star
57

grafana-zero

Python
2
star
58

gif

Simple GIF recorder
HTML
2
star
59

protoc-gen-micro

Protobuf code generation for micro
Go
2
star
60

scaffold

Templates for quick project start
Java
1
star
61

r

Something that rhymes. Or not.
1
star