A React implementation from scratch because why not? I hate my life and I hate JavaScript even more so I am on a path of self destruction
This project implements a functional version of React to understand the core principles behind React's design. It includes:
- Virtual DOM implementation
- Component creation with
createElement - Component rendering with
render - Class-based components with
Componentclass - Functional components using hooks
- State management with
setStateand hooks - Event handling
- Basic reconciliation algorithm
- Component lifecycle methods
- CSS styling integration
rereact/
├── public/
│ ├── index.html # Main HTML entry point
│ └── styles.css # Global styles
├── src/
│ ├── core/
│ │ ├── Component.js # Class component implementation
│ │ ├── Counter.js # Example class component
│ │ ├── createElement.js # Virtual DOM element creation
│ │ ├── hooks.js # Hooks implementation
│ │ ├── reconciler.js # DOM reconciliation logic
│ │ ├── render.js # DOM rendering implementation
│ │ └── Timer.js # Example functional component
│ └── index.js # Entry point and API exports
├── .babelrc
├── .gitignore
├── package.json
├── README.md
└── webpack.config.js
-
Install dependencies:
npm install -
Start the development server:
npm start -
Build for production:
npm run build
- Virtual DOM elements creation
- Basic rendering to the DOM
- Class components
- Component props
- State management
- Reconciliation algorithm
- Lifecycle methods
- Event handling
- Hooks API
- useState
- useEffect
- useRef
- useMemo
- useCallback
- Conditional rendering
- CSS styling integration
ReReact uses a virtual DOM approach similar to React. The virtual DOM is a lightweight JavaScript representation of the actual DOM that allows for efficient updates.
- Components are created using
createElementfunction - The rendering process converts virtual DOM to actual DOM elements
- For updates, a reconciliation process compares old and new virtual DOM trees and makes minimal changes
- Class components use the
setStatemethod - Functional components use the
useStatehook - Both trigger re-renders when state changes
Class components implement the standard lifecycle methods:
componentWillMountcomponentDidMountcomponentWillUpdatecomponentDidUpdatecomponentWillUnmount
Functional components can use hooks like:
useState- For state managementuseEffect- For side effects and lifecycle eventsuseRef- For persistent mutable valuesuseMemo- For memoized valuesuseCallback- For memoized callbacks