1.0.9 โ€ข Published 9 months ago

@ridhamsuhagiya/my-react-library v1.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

๐Ÿš€ 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

  1. Introduction
  2. Features
  3. Prerequisites
  4. How to Use
  5. Example
  6. Future Improvements
  7. Conclusion

๐ŸŽ‰ 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:

  1. Component ID: Every component needs a unique componentId prop. This helps the library keep track of your components. ๐Ÿ†”
  2. JSX Structure: Make sure the componentId is 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-folder

Step 2: Install Dependencies ๐Ÿ“ฆ

Make sure you have Node.js and npm installed. Then, run this command to install all the necessary dependencies:

npm install

This 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 start

This 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

  1. This is just the beginning! Here are some ideas for future enhancements:
  2. Performance Optimization: Make the Virtual DOM diffing algorithm faster and smarter. โšก
  3. More Hooks: Add hooks like useContext, useReducer, and useRef. ๐ŸŽฃ
  4. Error Handling: Improve error messages and debugging tools. ๐Ÿ›
  5. Component Lifecycle: Add more lifecycle methods to mimic Reactโ€™s class components. ๐Ÿ”„
  6. Async Rendering: Implement support for asynchronous rendering to improve performance. โณ
  7. Better TypeScript Support: Enhance TypeScript types for better developer experience. ๐Ÿ› ๏ธ
  8. Testing Framework: Add a testing framework to ensure reliability and stability. ๐Ÿงช

Happy coding, and may the hooks be with you! ๐Ÿง™โ€โ™‚๏ธโœจ

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago