Skip to content

yasiirdev/ReactJs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

244 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ReactJS - From Beginner to Pro πŸš€

Welcome to the comprehensive guide to ReactJS! This guide is structured to take you from the basics to advanced concepts in a clear, easy-to-follow format.

πŸ“š Table of Contents

🎯 Getting Started

What is React?

React is a JavaScript library for building user interfaces, particularly single-page applications. It's maintained by Meta (formerly Facebook) and a community of individual developers and companies.

Why React?

  • Component-Based: Build encapsulated components that manage their own state
  • Learn Once, Write Anywhere: Use React for web, mobile (React Native), and desktop apps
  • Virtual DOM: Efficient updates and rendering
  • Rich Ecosystem: Vast library of tools, extensions, and community support

Setting Up Your First React Project

# Using Vite (Recommended)
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev

# Using Create React App
npx create-react-app my-app
cd my-app
npm start

πŸ”₯ Core Concepts

1. JSX

JSX is a syntax extension for JavaScript that lets you write HTML-like code in your JavaScript files.

const element = <h1>Hello, {name}!</h1>;

2. Components

Components are the building blocks of React applications.

// Function Component (Recommended)
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

// className Component
className Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

3. Props

Props are read-only properties passed to components.

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

// Usage
<Welcome name="John" />;

4. State

State is a component's internal data that can change over time.

import { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

πŸš€ Advanced Topics

1. Hooks

Hooks let you use state and other React features in function components.

// Common Hooks
import { useState, useEffect, useContext, useRef } from "react";

// Custom Hook
function useWindowWidth() {
  const [width, setWidth] = useState(window.innerWidth);

  useEffect(() => {
    const handleResize = () => setWidth(window.innerWidth);
    window.addEventListener("resize", handleResize);
    return () => window.removeEventListener("resize", handleResize);
  }, []);

  return width;
}

2. Context API

Context provides a way to pass data through the component tree without passing props manually.

const ThemeContext = React.createContext("light");

function App() {
  return (
    <ThemeContext.Provider value="dark">
      <ThemedButton />
    </ThemeContext.Provider>
  );
}

3. React Router

Handle navigation in your React applications.

import { BrowserRouter, Routes, Route } from "react-router-dom";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
        <Route path="/users/:id" element={<UserProfile />} />
      </Routes>
    </BrowserRouter>
  );
}

πŸ’‘ Best Practices

  1. Component Organization

    • Keep components small and focused
    • Use functional components over className components
    • Follow the Single Responsibility Principle
  2. State Management

    • Lift state up when needed
    • Use Context API for global state
    • Consider Redux/Zustand for complex state management
  3. Performance

    • Use React.memo() for component memoization
    • Implement useMemo() and useCallback() when necessary
    • Lazy load components and routes
// Lazy Loading Example
const LazyComponent = React.lazy(() => import("./LazyComponent"));

function App() {
  return (
    <Suspense fallback={<Loading />}>
      <LazyComponent />
    </Suspense>
  );
}

πŸ“ Project Structure

src/
β”œβ”€β”€ components/     # Reusable components
β”œβ”€β”€ pages/         # Page components
β”œβ”€β”€ hooks/         # Custom hooks
β”œβ”€β”€ context/       # Context providers
β”œβ”€β”€ services/      # API calls and services
β”œβ”€β”€ utils/         # Helper functions
β”œβ”€β”€ assets/        # Images, fonts, etc.
└── styles/        # Global styles

⚑ Performance Optimization

  1. Code Splitting

    • Use dynamic imports
    • Implement route-based code splitting
    • Lazy load heavy components
  2. Rendering Optimization

    • Use React.memo() for pure components
    • Implement virtual scrolling for long lists
    • Optimize images and assets
  3. State Management

    • Use local state when possible
    • Implement efficient global state management
    • Avoid prop drilling

πŸš€ Deployment

  1. Build Process
npm run build
  1. Hosting Options
    • Vercel (Recommended for Next.js)
    • Netlify
    • GitHub Pages
    • AWS Amplify

πŸ”— Additional Resources

🀝 Contributing

Feel free to contribute to this guide by submitting pull requests or creating issues for improvements and corrections.


Happy Coding! πŸŽ‰

Remember to ⭐ this repository if you find it helpful!

Releases

No releases published

Packages

 
 
 

Contributors