@ridhamsuhagiya/my-react-library v1.0.9
๐ Custom React Implementation ๐
Welcome to the world of custom React! This project is a fun and simplified implementation of React, designed to help you
understand the magic behind hooks like useState, useEffect, conditional rendering, and cleanup functions. Whether
you're a React enthusiast or just curious about how frameworks work under the hood, this project is for you! Let's dive
in and build something awesome together! ๐ ๏ธ
๐ Table of Contents
๐ Introduction
Ever wondered how React works behind the scenes? ๐ค This project is your chance to explore a custom React-like library
built from scratch! While itโs not as powerful as the real React, itโs packed with core features like useState,
useEffect, and conditional rendering. Think of it as Reactโs little siblingโsmall, fun, and full of potential! ๐ง
โจ Features
Hereโs what you can do with this custom React implementation:
- useState: Manage state like a pro! ๐๏ธ
- useEffect: Handle side effects and cleanups like a boss. ๐งน
- Conditional Rendering: Show or hide components based on state. ๐ญ
- Component Lifecycle: Basic lifecycle management with cleanup functions. ๐
- Virtual DOM: A simple Virtual DOM for efficient updates. ๐
๐ ๏ธ Prerequisites
Before you start, here are a few things to keep in mind:
- Component ID: Every component needs a unique
componentIdprop. This helps the library keep track of your components. ๐ - JSX Structure: Make sure the
componentIdis at the top of your JSX structure. Itโs like giving your component a name tag! ๐ท๏ธ
๐ How to Use
Ready to get started? Hereโs how:
Step 1: Clone the Repository ๐
First, grab the code and make it yours! Run this command in your terminal:
git clone https://github.com/your-repo-url.git
cd your-repo-folderStep 2: Install Dependencies ๐ฆ
Make sure you have Node.js and npm installed. Then, run this command to install all the necessary dependencies:
npm installThis will install everything you need, including lodash, parcel, and typescript. ๐ ๏ธ
Step 3: Start the Development Server ๐
Now, letโs fire up the development server! Run this command:
npm startThis will start the app using Parcel, and you can view it in your browser at http://localhost:1234. ๐
Step 5: Create Your Own Components ๐งฉ
Now that everything is set up, you can start creating your own components! Follow the example in the index.ts file, and donโt forget to add a componentId to each component. ๐จ
๐จ Example
Hereโs a sneak peek of how to use this custom React implementation:
import React from "./react";
export const APP = () => {
const { createElement, useState, mount, useEffect } = React();
const Timer = () => {
const [time, setTime] = useState(new Date().toLocaleTimeString());
const [isRunning, setIsRunning] = useState(true);
useEffect(() => {
let interval: any;
if (isRunning) {
interval = setInterval(() => {
setTime(new Date().toLocaleTimeString());
}, 1000);
}
return () => {
if (interval) {
console.log("Cleaning up timer");
clearInterval(interval);
}
};
}, [isRunning]);
return createElement(
"div",
{ className: "test-component", componentId: "timer-component" },
createElement("h2", null, "Test 1: useEffect with Cleanup"),
createElement("p", null, `Current time: ${time}`),
createElement(
"button",
{
onClick: () => setIsRunning(!isRunning),
className: "button",
},
isRunning ? "Stop Timer" : "Start Timer",
),
createElement("p", { className: "effect-note" }, "Check console for effect logs"),
);
};
const AppComponent = () => {
const [showTests, setShowTests] = useState(true);
return createElement(
"div",
{ className: "app", componentId: "main-app-component" },
createElement("h1", null, "useEffect Test Cases"),
createElement(
"p",
{ className: "test-guide" },
"Open the browser console to see effect lifecycles in action",
),
createElement(
"button",
{
onClick: () => setShowTests((prev: any) => !prev),
className: "toggle-button",
},
showTests ? "Hide All Tests" : "Show All Tests",
),
showTests &&
createElement(
"div",
{ className: "test-cases" },
{ componentFunc: Timer, componentId: "timer-component" },
),
);
};
mount({ componentFunc: AppComponent, componentId: "main-app-component" });
};
APP();๐ง Future Improvements
- This is just the beginning! Here are some ideas for future enhancements:
- Performance Optimization: Make the Virtual DOM diffing algorithm faster and smarter. โก
- More Hooks: Add hooks like useContext, useReducer, and useRef. ๐ฃ
- Error Handling: Improve error messages and debugging tools. ๐
- Component Lifecycle: Add more lifecycle methods to mimic Reactโs class components. ๐
- Async Rendering: Implement support for asynchronous rendering to improve performance. โณ
- Better TypeScript Support: Enhance TypeScript types for better developer experience. ๐ ๏ธ
- Testing Framework: Add a testing framework to ensure reliability and stability. ๐งช
Happy coding, and may the hooks be with you! ๐งโโ๏ธโจ