Core React Dependencies

0
220

React dependencies can be categorized into core dependencies, state management, routing, styling, data fetching, utility libraries, and build tools. Below is a breakdown with explanations and examples.


1. Core Dependencies

These are the essential dependencies for a React project.

🔹 React (react)

  • The main library for building UI components.

  • Example:

    import React from 'react';
    
    function App() {
      return <h1>Hello, React!</h1>;
    }
    
    export default App;
    

🔹 React DOM (react-dom)

  • Used for rendering components to the browser.

  • Example:

    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App';
    
    ReactDOM.createRoot(document.getElementById('root')).render(<App />);
    

2. State Management Dependencies

These help manage the global state of your app.

🔹 Redux (@reduxjs/toolkit, react-redux)

  • A centralized state management tool.

  • Example:

    import { configureStore } from '@reduxjs/toolkit';
    import { Provider, useDispatch, useSelector } from 'react-redux';
    
    const store = configureStore({ reducer: {} });
    
    function App() {
      return (
        <Provider store={store}>
          <h1>Redux App</h1>
        </Provider>
      );
    }
    
    export default App;
    

🔹 Zustand

  • Lightweight alternative to Redux.

  • Example:

    import create from 'zustand';
    
    const useStore = create((set) => ({
      count: 0,
      increment: () => set((state) => ({ count: state.count + 1 })),
    }));
    
    function Counter() {
      const { count, increment } = useStore();
      return (
        <div>
          <p>Count: {count}</p>
          <button onClick={increment}>Increment</button>
        </div>
      );
    }
    

3. Routing Dependencies

These handle navigation in React apps.

🔹 React Router (react-router-dom)

  • Enables client-side navigation.

  • Example:

    import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
    
    function Home() {
      return <h1>Home Page</h1>;
    }
    
    function About() {
      return <h1>About Page</h1>;
    }
    
    function App() {
      return (
        <BrowserRouter>
          <nav>
            <Link to="/">Home</Link>
            <Link to="/about">About</Link>
          </nav>
          <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/about" element={<About />} />
          </Routes>
        </BrowserRouter>
      );
    }
    
    export default App;
    

4. Styling Dependencies

Used for styling components.

🔹 Styled Components (styled-components)

  • Allows styling with tagged template literals.

  • Example:

    import styled from 'styled-components';
    
    const Button = styled.button`
      background-color: blue;
      color: white;
      padding: 10px;
      border-radius: 5px;
    `;
    
    function App() {
      return <Button>Click Me</Button>;
    }
    

🔹 Tailwind CSS

  • Utility-first CSS framework.

  • Example:

    function App() {
      return <button className="bg-blue-500 text-white p-2 rounded">Click Me</button>;
    }
    

5. Data Fetching Dependencies

These help fetch and manage API data.

🔹 Axios (axios)

  • Used for making HTTP requests.

  • Example:

    import { useEffect, useState } from 'react';
    import axios from 'axios';
    
    function App() {
      const [data, setData] = useState([]);
    
      useEffect(() => {
        axios.get('https://jsonplaceholder.typicode.com/posts')
          .then(response => setData(response.data));
      }, []);
    
      return (
        <ul>
          {data.map(post => <li key={post.id}>{post.title}</li>)}
        </ul>
      );
    }
    

🔹 React Query (@tanstack/react-query)

  • Handles caching and background fetching.

  • Example:

    import { useQuery } from '@tanstack/react-query';
    import axios from 'axios';
    
    function fetchPosts() {
      return axios.get('https://jsonplaceholder.typicode.com/posts').then(res => res.data);
    }
    
    function App() {
      const { data, error, isLoading } = useQuery(['posts'], fetchPosts);
    
      if (isLoading) return <p>Loading...</p>;
      if (error) return <p>Error fetching data</p>;
    
      return (
        <ul>
          {data.map(post => <li key={post.id}>{post.title}</li>)}
        </ul>
      );
    }
    

6. Utility Dependencies

Useful for various functions in React apps.

🔹 Lodash (lodash)

  • Provides utility functions.

  • Example:

    import _ from 'lodash';
    
    const numbers = [1, 2, 3, 4, 5];
    const doubled = _.map(numbers, (num) => num * 2);
    console.log(doubled); // [2, 4, 6, 8, 10]
    

🔹 Moment.js (moment)

  • Used for date formatting.

  • Example:

    import moment from 'moment';
    
    function App() {
      return <p>{moment().format('MMMM Do YYYY, h:mm:ss a')}</p>;
    }
    

7. Build and Testing Dependencies

These assist in development and testing.

🔹 Webpack (webpack)

  • A module bundler for React.

  • Example config (webpack.config.js):

    module.exports = {
      entry: './src/index.js',
      output: {
        filename: 'bundle.js',
      },
      mode: 'development',
    };
    

🔹 Babel (@babel/preset-react)

  • Transpiles JSX into JavaScript.

  • Example .babelrc:

    {
      "presets": ["@babel/preset-react"]
    }
    

🔹 Jest (jest)

  • A JavaScript testing framework.

  • Example test:

    test('adds 1 + 2 to equal 3', () => {
      expect(1 + 2).toBe(3);
    });
    

🔹 React Testing Library (@testing-library/react)

  • Used for testing React components.

  • Example:

    import { render, screen } from '@testing-library/react';
    import App from './App';
    
    test('renders Hello, React!', () => {
      render(<App />);
      expect(screen.getByText('Hello, React!')).toBeInTheDocument();
    });
    

📌 Key Points

✅ React (react) and React DOM (react-dom) are core dependencies.
✅ Redux, Zustand, and React Query help manage state.
✅ React Router is essential for navigation.
✅ Styled Components and Tailwind CSS handle styling.
✅ Axios and React Query are used for fetching data.
✅ Lodash and Moment.js are utility libraries.
✅ Webpack, Babel, and Jest are crucial for development and testing.

Would you like more details on any of these? 🚀

Search
Sellect from all Categories bellow ⇓
Read More
Web Development
What we mean by Server-side programming
Server-side programming refers to the...
By flowisetech 2025-01-28 12:09:28 0 832
Website Promotion
How to Define Your Target Audience in Promoting Your Social Media Website
Before promoting your social media website, you...
By flowisetech 2025-03-09 20:30:31 0 650
JavaScript
How to Optimize JavaScript for Better Web Performance
Here's a detailed guide on optimizing...
By flowisetech 2025-03-07 09:47:59 0 463
Business
How to Start an E-commerce Business from Scratch in 2025
Introduction Starting an e-commerce business...
By flowisetech 2025-02-25 11:54:02 0 349
Mobile App Development
PWA vs Native App: Which One to Build?
Choosing between a Progressive Web App (PWA)...
By flowisetech 2025-04-12 09:49:42 0 37