A custom React-like library implementation written in TypeScript, featuring a fiber-based reconciliation engine and hooks system.
Akaza is a lightweight React-like library that implements core React concepts from scratch, including:
- Fiber Architecture: A fiber-based reconciliation system for efficient rendering
- Hooks System: Support for useState, useSideEffect (useEffect), useRef, and useContext
- Virtual DOM: Virtual node system with reconciliation and diffing
- Context API: Component context sharing system
- Error Boundaries: Error handling and recovery mechanisms
- JSX Support: Full JSX syntax support via Babel
- Fiber: The fundamental unit of work representing a component or DOM node
- Runtime: Global state management for the reconciliation process
- Work Loop: Time-sliced rendering using
requestIdleCallback
- render() (
src/utils/render.ts
): Entry point that sets up the fiber tree - workLoop(): Processes fibers incrementally during idle time
- performBitOfWork() (
src/utils/performBitOfWork.ts
): Handles individual fiber processing - reconcileChildren() (
src/utils/reconcileChildren.ts
): Manages child fiber reconciliation - commitRoot() (
src/utils/dom/commitRoot.ts
): Applies changes to the actual DOM
- useState (
src/hooks/useState.ts
): State management with queue-based updates - useSideEffect (
src/hooks/useSideEffect.ts
): Side effects with dependency tracking - useRef (
src/hooks/useRef.ts
): Mutable reference objects - useContext (
src/hooks/useContext.ts
): Context consumption with provider lookup
- createElement (
src/utils/dom/createElement.ts
): Virtual node creation - createDom (
src/utils/dom/createDom.ts
): Actual DOM node creation - updateDom (
src/utils/dom/updateDom.ts
): Efficient DOM property updates
Implements a work-in-progress (WIP) fiber tree system that allows for:
- Interruptible rendering
- Priority-based updates
- Efficient reconciliation with previous renders
- Time-slicing for better performance
Full implementation of React-like hooks with:
- Hook Order Validation: Ensures consistent hook ordering across renders
- State Queue Processing: Batches state updates efficiently
- Dependency Tracking: Optimizes side effect execution
- Context Traversal: Walks fiber tree to find context providers
Comprehensive error handling system:
- Catches errors during component rendering
- Provides fallback UI components
- Propagates errors up the component tree
- Implements error recovery mechanisms
src/
├── akaza.ts # Main exports
├── index.tsx # Sample application
├── runtime.ts # Global runtime state
├── types.ts # TypeScript definitions
├── hooks/ # Hook implementations
│ ├── useContext.ts
│ ├── useRef.ts
│ ├── useSideEffect.ts
│ └── useState.ts
└── utils/ # Core utilities
├── dom/ # DOM manipulation
├── performBitOfWork.ts
├── reconcileChildren.ts
├── render.ts
└── error-boundary.ts
- TypeScript: Full TypeScript support with strict typing
- Babel: Transpilation with JSX transformation
- JSX Runtime: Custom JSX factory function
- ES Modules: Modern module system
npm run dev
: Run development server with hot reloadingnpm run build
: Build for productionnpm run serve
: Serve built applicationnpm run run
: Build and run application
The project includes a sample application (src/index.tsx
) demonstrating:
- Component state management
- Side effects with cleanup
- Context API usage
- Ref handling
- Event handling
Each fiber contains:
dom
: Reference to actual DOM nodetype
: Component function or HTML element typeprops
: Component propertiesparent/child/sibling
: Tree navigation pointersalternate
: Previous render comparisoneffectTag
: Change type (UPDATE/PLACEMENT/DELETION)hooks
: Array of hook instances
Hooks rely on:
- Consistent ordering across renders
- Index-based hook retrieval from previous renders
- Queue-based state updates
- Dependency comparison for side effects
Contexts use:
- Symbol-based identification
- Provider component tagging
- Fiber tree traversal for value lookup
- Default value fallback
- useReducer: State management with reducer pattern (built as a wrapper around useState)
- useMemo: Memoization for expensive computations with dependency tracking
- useCallback: Memoization for function references to prevent unnecessary re-renders
- useLayoutEffect: Synchronous effects that run before browser paint (commit-time ordering)
- Controlled Inputs: Improved synchronization of input values and checked states with proper event mapping
- Keyed Moves: Advanced reconciliation that detects element reorders and uses
insertBefore
instead of detach/append operations - Efficient List Rendering: Better handling of dynamic lists with minimal DOM operations
- Stricter Hook Validation: Enhanced hook order checking with better error messages
- Missing Key Warnings: Development-time warnings for missing keys in lists
- Enhanced Error Boundaries: More detailed error reporting and recovery mechanisms
These features will further enhance Akaza's React compatibility while maintaining its educational value for understanding modern framework internals.