A minimal React-like UI library built from scratch to understand how the virtual DOM, reconciliation, and hooks actually work.
This is a learning project, not a production framework. It implements the core ideas behind React—functional components, a virtual DOM, a reconciliation engine, hooks (useState, useEffect, useReducer, useRef, useMemo, useCallback), and the Context API—without the complexity of the real thing. The goal is to write code that is small enough to read in one sitting, but complete enough to actually build UIs with.
Requires Bun.
git clone https://github.com/MarcelOlsen/mini-react.git
cd mini-react
bun install
bun testimport { createElement, render, useState } from "@marcelolsen/mini-react";
const Counter = () => {
const [count, setCount] = useState(0);
return createElement(
"button",
{ onClick: () => setCount(count + 1) },
`Count: ${count}`
);
};
render(createElement(Counter), document.getElementById("root")!);Configure your build tool to use the MiniReact JSX runtime:
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "@marcelolsen/mini-react"
}
}Then write components normally:
const App = () => {
return (
<div>
<h1>Hello</h1>
<Counter />
</div>
);
};- Virtual DOM & Reconciliation: Diff and patch the DOM efficiently.
- Functional Components: Props, children, and composition.
- Hooks: useState, useEffect, useReducer, useRef, useMemo, useCallback.
- Context API: createContext / useContext for passing data through the tree.
- Portals: Render children into a different DOM container while keeping the React tree structure.
- Fragments: Group children without wrapper nodes.
- JSX Runtime: Production and development JSX transforms (jsx, jsxs, jsxDEV).
- Events: Standard DOM events attached directly to nodes.
- Performance: Basic memoization via
memo,useMemo, anduseCallback.
src/
├── MiniReact.ts # Main exports and JSX runtime
├── types.ts # TypeScript definitions
├── vdom.ts # Virtual DOM creation
├── reconciler.ts # Reconciliation / diffing engine
├── hooks.ts # Hook implementations
├── context.ts # Context API
├── portals.ts # Portals
├── events.ts # Event system
└── jsx/
├── jsx-runtime.ts
└── jsx-dev-runtime.ts
This project is built in incremental phases. Each phase has a clear goal, an implementation, and tests.
- Element Creation & Basic Rendering
- Functional Components
- Virtual DOM & Reconciliation
- Prop Diffing & Children Reconciliation
- State with useState
- Event Handling
- Effects with useEffect
- Context API
- Portals and Fragments
- JSX Support
- useRef & useReducer
- Performance Optimization Suite — memo, useMemo, useCallback
- Error Boundaries & Resilience
- Async Features & Suspense
- Concurrent Features
- Developer Experience
- Server-Side Rendering
- Advanced Component Patterns
- Testing & Quality Assurance
- Production Optimizations
Creates a virtual DOM element.
const el = createElement("div", { id: "app" }, "Hello");Renders a virtual element into a real DOM container.
render(createElement(App), document.getElementById("root")!);Returns a state tuple [value, setValue].
const [count, setCount] = useState(0);Runs side effects after render. Return a cleanup function if needed.
useEffect(() => {
const id = setInterval(() => setTime(t => t + 1), 1000);
return () => clearInterval(id);
}, []);State management with a reducer function.
const [state, dispatch] = useReducer(counterReducer, { count: 0 });Mutable reference that persists across renders without causing re-renders.
const inputRef = useRef<HTMLInputElement>(null);Memoize expensive computations and stable function references.
Create and consume context to avoid prop drilling.
const ThemeContext = createContext("light");
const theme = useContext(ThemeContext);Render children into a different DOM node.
createPortal(createElement(Modal), document.getElementById("modal-root")!);Group multiple elements without adding a wrapper to the DOM.
createElement(Fragment, null, child1, child2);Tests run with Bun and use happy-dom for DOM simulation.
bun test # run all tests
bun test --watch # watch mode
bun test --coverage # with coverageLinting and formatting with Biome:
bunx biome check
bunx biome check --applyMIT
Built to learn. Read the code, break it, fix it, understand it.