What is React? Challenging Your Knowledge by Going Beyond...

Challenging What You Thought You Knew by Going Beyond the Surface!

Β·

15 min read

What is React? Challenging Your Knowledge by Going Beyond...

πŸ” In life, having answers to the what, why, and how of your goals greatly increases your chances of success. πŸ†

🎯 Let's consider the goal of becoming a front-end developer. It's crucial to know what frontend development entails, why you want to pursue it, and how you plan to achieve it. 🌟

πŸ€” However, when it comes to learning new technologies, we often overlook these important aspects. But don't worry, in this blog, we won't skip them. πŸ“š

πŸ” Instead, we'll delve into what React is, why it came into existence, its purpose, and how it fulfills that purpose through core principles. We'll also explore its current limitations. πŸš€

πŸ’‘ But don't worry, this won't be just another definition-heavy blog. πŸ“– Instead, we'll understand React through real-world analogies and explore optimal and suboptimal examples. We'll even tackle interview-related questions and provide hands-on exercises to reinforce your knowledge. πŸ’ͺ

⌨️ By having answers to these questions, you'll approach coding with a clear mindset, avoiding the pitfalls of crashing or encountering unexpected behaviors. 🚧

Don't forget to share your own "why" in the comment section!

Introduction

🌟 Welcome to my blog series! πŸ†•

I'm on a mission to provide a comprehensive and practical understanding of React. πŸš€

By the end of the Mastering React series, you'll gain the knowledge and confidence to excel in React and overcome any obstacles. πŸ’ͺ

Stay updated by following me along with subscribing to the newsletter so you Don't miss out on another blog!

Let's dive into React together and embark on an exciting journey! 🌟

What is React? πŸ€”

React is a powerful JavaScript library used for building user interfaces. 🌟 It offers a declarative and efficient approach to creating interactive UI components.

Imagine React as a collection of building blocks🧱 that you can use to construct a house. Each block represents a React component, and when you combine these components, you can build a fully functional and interactive UI. 🏠

As you delve deeper into React, you'll learn how to optimize this construction process for efficient and effective UI development. πŸ’ͺ

Overview of React Purpose (Why) ❓

React was developed to overcome several limitations and challenges faced by previous web development technologies. These limitations include:

  1. Complex HTML manipulation: Traditional frameworks required intricate manipulation of HTML, leading to complex and convoluted code.

  2. Inefficient rendering: Dynamic UI updates were cumbersome, resulting in poor rendering performance.

  3. Lack of reusability: Previous approaches lacked modularity, making it difficult to reuse code effectively.

  4. Inconsistent data management: Handling application state was challenging, often leading to data inconsistencies.

  5. Imperative code complexity: Imperative programming approaches resulted in bugs and difficulties in maintenance.

  6. Weak support for single-page applications (SPAs): Existing technologies struggled with the efficient development of SPAs.

React addresses these limitations by introducing a component-based architecture, a virtual DOM for efficient updates, a one-way data flow for predictable state management, and a declarative programming approach. πŸš€

React Core Principles (How) πŸ—οΈ

Let's imagine you're constructing a house. React serves as the construction framework, helping you build different components of the house, such as walls, doors, and windows. 🏠

  • Component-Based Architecture: In React, you break down your house into smaller components, just like Lego blocks. Each component represents a specific part of the house, such as a Wall component, a Door component, or a Window component. These components can be reused in different houses, similar to how Lego blocks can build various structures.

  • Virtual DOM and Reconciliation Process: React's Virtual DOM acts as a blueprint for your house. Instead of making changes directly to the physical house, React updates the blueprint first. It then intelligently determines which parts of the house need modifications and efficiently applies those changes. This process, known as reconciliation, ensures only necessary updates are made, resulting in faster and smoother construction.

  • Unidirectional Data Flow: Imagine data flow in React as instructions from an architect to construction workers. The architect (parent component) provides instructions to the workers (child components) on how to build each component. The workers follow these instructions and construct their respective components accordingly. This one-way flow of instructions simplifies the construction process and maintains consistency.

  • Declarative Approach: React's declarative approach is like to having an interior designer decorate your house. Rather than specifying every detail of how to decorate each room, you describe the desired outcome or ambiance. The interior designer (React) takes care of the underlying logic and applies the decorations accordingly. This allows you to focus on the overall vision without getting caught up in the specifics.

  • Immutability: In React, immutability is like having an unchanging blueprint. When you need to make changes, you create a new blueprint with the desired modifications. Similarly, in React, you create new objects or arrays when updating data instead of directly modifying existing ones. This ensures the state remains consistent and facilitates tracking changes.

Throughout this blog series, we'll illustrate each core principle using simple code examples. Don't worry if you're new to React or have never written a line of code before. The code itself will be self-explanatory, and you can try to understand it. Later on, we'll provide explanations to enhance your understanding. πŸ“

React Component-Based Architecture πŸš—

Let's imagine you're constructing a car using a component-based approach in React. πŸš—

  • Understanding the concept of components in React: In React, components serve as the building blocks of your car. Each component represents a specific part of the car, such as the engine, wheels, seats, and dashboard. Every component is responsible for its own functionality and appearance, making the car a cohesive and organized structure.

  • Benefits of a component-based approach: Embracing the component-based approach offers several advantages in building your car:

    1. Reusability: Like components in React, certain parts of the car, such as the engine or wheels, can be reused across different car models. This eliminates duplication and saves time and effort in the manufacturing process.

    2. Modularity: Components in React are independent and self-contained, allowing for easier maintenance and updates. Similarly, in the car manufacturing process, each component can be developed and tested separately, facilitating troubleshooting and continuous improvement.

    3. Encapsulation: React components encapsulate their own logic and data, preventing interference with other components. Similarly, in the car, each component has its own internal mechanisms and functions, ensuring their functionalities work independently. For instance, the engine component handles power and performance without affecting other components like the seats or dashboard.

    4. Collaboration: React enables multiple developers to work simultaneously on different components, fostering faster development and collaboration. Similarly, in the car manufacturing process, specialized teams can work on specific components, such as the electrical system or suspension, ensuring expertise and efficiency in each area.

As you proceed to create your next component, keep these benefits in mind, rather than merely fulfilling the component's immediate task. By doing so, you can harness the power of component-based architecture effectively.

It may seem like a lot of theory, but don't worry! We'll soon dive into practical examples to solidify your understanding. πŸ› οΈ

Here's the simplest code example that demonstrates the benefits of React component-based architecture by creating a simple car dashboard component:

Bad Code Example - Not Leveraging Benefits: πŸ‘Ž

// CarDashboard.js
import React from 'react';

function CarDashboard() {
  const speed = 80;
  const fuelLevel = 50;

  return (
    <div>
      <h2>Car Dashboard</h2>
      <p>Speed: {speed}</p>
      <p>Fuel Level: {fuelLevel}</p>
    </div>
  );
}

export default CarDashboard;

Explanation:

In the above "Bad" code example, we do not leverage the benefits of React's component-based architecture. Instead, we directly render the speed and fuel level as plain text within the CarDashboard component. This approach lacks reusability, modularity, and encapsulation, making it harder to maintain and extend the codebase.

Example of Optimal Code - Leveraging Benefits: πŸ‘

// CarDashboard.js
import React from 'react';
import Speedometer from './Speedometer';
import FuelGauge from './FuelGauge';

function CarDashboard() {
  return (
    <div>
      <h2>Car Dashboard</h2>
      <Speedometer speed={80} />
      <FuelGauge fuelLevel={50} />
    </div>
  );
}

export default CarDashboard;

In the above code, we have a CarDashboard component that leverages the benefits of React's component-based architecture. It composes of smaller, reusable components (Speedometer and FuelGauge) to build a complete car dashboard UI. Each component focuses on a specific functionality (speed and fuel level) and encapsulates its own state and rendering logic.

Do answer this question in your way to reinforce your learning.

Interview Questions:

  1. What advantages does React's component-based architecture offer?

  2. How does the component-based approach in React promote code reusability?

  3. Can you explain the concept of encapsulation in React components?

  4. In what ways does React's component-based architecture facilitate collaboration among developers?

Hands-on Exercise:

πŸ”§ Let's reinforce your learning with a hands-on exercise. Follow these steps:

  1. πŸ—οΈ Create a new React project using Create React App or your preferred method.

  2. πŸ› οΈ Implement a Speedometer component that receives the current speed as a prop and displays it on the UI.

  3. πŸ› οΈ Implement a FuelGauge component that receives the fuel level as a prop and displays it on the UI.

  4. πŸ”„ Use the Speedometer and FuelGauge components within the CarDashboard component.

  5. πŸ–₯️ Render the CarDashboard component in your application and observe the displayed speed and fuel level.

πŸ’‘ After completing the exercise, don't forget to share your progress by tagging me with #ReactWithHarshal on socials. Your engagement is valuable in assessing the impact of this content and ensuring its effectiveness in supporting your learning journey. Additionally, feel free to reach out if you have any questions or need mentoring, project review, or consultation assistance. Keep up the great work, and happy coding!

Virtual DOM and Reconciliation Process πŸ“š

Imagine you're a librarianπŸ§‘β€πŸ« managing a library with a large collection of books using a virtual catalog and an efficient updating process.

  • Explanation of Virtual DOM and its role in React: React's Virtual DOM can be likened to a virtual catalog for your library. It serves as a lightweight representation of the actual DOM (Document Object Model) - the comprehensive structure of your library, consisting of countless books. The Virtual DOM enables React to keep track of changes made to the library in an efficient manner.

    Just as a virtual catalog allows you to perform operations like searching for specific books or updating the catalog without physically handling each book, the Virtual DOM acts as an intermediary between your application's logic and the real DOM. React can interact with the Virtual DOM, perform necessary operations, and optimize the updating process without directly manipulating the real DOM.

  • How React efficiently updates the DOM through reconciliation: Let's consider the scenario of adding a new bookπŸ“š to your library. React employs a highly efficient updating process known as reconciliation to handle such changes.

    When a change occurs, React compares the virtual catalog (Virtual DOM) with the physical catalog (real DOM) to identify differences or updates that need to be made. However, instead of meticulously inspecting every bookshelf and book, React utilizes its reconciliation algorithm to optimize the process.

    By determining the minimum set of changes required based on the disparities between the Virtual DOM and the real DOM, React focuses solely on the relevant bookshelfπŸ“š where the new book is added. This targeted approach allows React to update the catalog efficiently, avoiding unnecessary modifications to unchanged bookshelves or books.

    This reconciliation process saves time⏱️ and resources, ensuring a more streamlined and efficient updating mechanism.

By drawing parallels between React's Virtual DOM and reconciliation process and the management of a library with a virtual catalog, we gain a clearer understanding of how React optimizes the updating of the real DOM through selective and efficient changes.

Thanks! for sticking to reading this far If you found this blog helpful, please show your support by liking and commenting. Your encouragement motivates me to write more blogs and helps spread the word to fellow developers.

To stay updated and follow this blog series, don't forget to follow me and subscribe to the newsletter. Stay tuned so you don't miss another blog!

Now, let's delve into a code example that demonstrates the concept of the Virtual DOM and the reconciliation process in React.

Example of Bad Code: πŸ‘Ž

import React, { useState } from 'react';

// Library component
const Library = () => {
  const [books, setBooks] = useState([
    { id: 1, title: 'Book 1' },
    { id: 2, title: 'Book 2' },
    { id: 3, title: 'Book 3' },
  ]);

  // Function to add a new book
  const addBook = () => {
    const newBook = `Book ${books.length + 1}`;
    books.push(newBook); // BAD: Directly modifying the original array
    setBooks(books); // BAD: Setting the same array reference
  };

  return (
    <div>
      <h2>Library</h2>
      <ul>
        {books.map((book) => (
          <li key={"bookid"}>{book.title}</li>
        ))}
      </ul>
      <button onClick={addBook}>Add Book</button>
    </div>
  );
};

export default Library;

Explanation:

This code snippet showcases a suboptimal implementation of the bookshelf component, which can lead to issues and unexpected behavior. Let's examine the problems and discuss their implications:

  1. Mutation of the original books array: The code directly modifies the original books array using the push method. This violates React principle of immutability, which recommends creating new objects or arrays instead of modifying existing ones. By mutating the array, the component loses track of changes and may not trigger proper re-rendering, resulting in an incorrect UI representation.

  2. Ineffective state update: The setBooks(books) call sets the state with the same array reference. Since React relies on shallow equality checks to determine state updates, it may not recognize the change and skip the re-rendering process. Consequently, the UI fails to reflect the addition of the new book accurately. This issue is common among beginner developers who may overlook the importance of immutability and state updates.

  3. Incorrect usage of the key prop: Within the Library component, there is a list of books rendered. However, the key prop, which is crucial for React reconciliation process, is not correctly assigned. Properly assigning unique keys to list items enables React to efficiently identify and update changes within the list. Neglecting the key assignment can lead to performance problems and difficulties in accurately tracking changes.

Example of Optimal Code: πŸ‘

import React, { useState } from 'react';

// Bookshelf component
const Bookshelf = ({ books }) => {
  return (
    <ul>
      {books.map((book) => (
        <li key={book.id}>{book.title}</li>
      ))}
    </ul>
  );
};

// Library component
const Library = () => {
  const [books, setBooks] = useState([
    { id: 1, title: 'Book 1' },
    { id: 2, title: 'Book 2' },
    { id: 3, title: 'Book 3' },
  ]);

  const addBook = () => {
    const newBook = { id: 4, title: 'Book 4' };
    //Update data instead of directly modifying the existing ones
    setBooks([...books, newBook]); 
  };

  return (
    <div>
      <h2>Library</h2>
      <Bookshelf books={books} />
      <button onClick={addBook}>Add Book</button>
    </div>
  );
};

export default Library;

Explanation:

In this example, we have a Library component that manages a list of books. The Library component renders a Bookshelf component, which represents a collection of books. Whenever the "Add Book" button is clicked, a new book is added to the list of books using the addBook function.

React's Virtual DOM and reconciliation process come into play when a book is added. React efficiently updates the DOM by comparing the previous Virtual DOM with the updated Virtual DOM. It identifies the specific changes (in this case, the addition of a new book) and only updates the necessary parts of the DOM, in this case, the <ul> element within the Bookshelf component.

Interview Questions: ❓

  1. πŸ€” What is the purpose of the Virtual DOM in React and why is it used?

  2. πŸ€” Can you explain the reconciliation process in React and how it improves DOM updates?

  3. πŸ€” How does React determine the minimal set of changes needed during reconciliation?

  4. πŸ€” What are the advantages of using the Virtual DOM and reconciliation in terms of performance and user experience?

  5. πŸ€” How does React efficiently handle additions or removals of items in a list?

Hands-on Exercise:

πŸ”§ Let's dive into some hands-on practice to reinforce your understanding of the Virtual DOM and reconciliation in React:

  1. πŸ—οΈ Set up a new React project using Create React App or your preferred method.

  2. πŸ“ Replace the default content of the App component with the provided code snippet for the Library component.

  3. ▢️ Run the React application and observe how the initial list of books is rendered.

  4. βž• Click the "Add Book" button and witness how React efficiently updates the DOM by adding the new book to the bookshelf without re-rendering the entire component.

By actively engaging in the code examples and answering the interview questions, you'll solidify your comprehension of the Virtual DOM, the reconciliation process, and their vital role in React development. The hands-on exercise enables you to witness these concepts in action, enhancing your learning experience.

Advantages of Using React πŸš€

If you've made it this far, you can figure out the advantages of using React. I will encourage you to take a moment and Feel free to share it in the comments section as well.

Considering the length of the blog so far I will keep this section shorter and concise and will write another blog covering in-depth react limitations with practical examples, how to tackle those limitations, interview-related questions, and hands-on exercises.

Understanding React LimitationsπŸ€·β€β™€οΈ

React, while powerful, has certain limitations and trade-offs to consider:

  1. Learning Curve: React has a learning curve, especially for beginners. It requires understanding concepts like components, props, state, and JSX syntax.

  2. Complexity for Small Projects: React's complexity may be unnecessary for small, simple projects.

  3. Performance Impact: Large-scale applications or complex component hierarchies in React may require optimization techniques to maintain performance.

  4. Mobile Development: React Native is used for mobile app development, but highly performance-critical or platform-specific apps may benefit from native development or specialized frameworks.

  5. SEO Limitations: JavaScript-rendered content in React apps can pose challenges for search engine optimization (SEO). Techniques like server-side rendering (SSR) or pre-rendering can mitigate this.

  6. Tooling and Ecosystem: Staying up-to-date with React's ecosystem and tools can be overwhelming due to the rapidly evolving JavaScript landscape.

Summary ✨

πŸ“š In this blog, we covered the what, why, how, and limitations of React. It provides a strong foundation in React, enabling you to write better, more optimal code by keeping the core principles in mind. It's important to remember that if you're a beginner, don't stress too much about writing the most optimal code right from day one. You'll learn and improve over time as you work on projects. Before diving into building components or features, make sure to thoroughly understand the requirements, just like how we tackle programming problems. Start with pseudo code and implement it using a brute force approach, and then optimize it later. Always remember the golden rule: "If it works, don't touch it πŸ˜…." kidding But before starting optimization, make sure to have a backup of your working code. Happy coding! If you found this blog helpful, show your support for it. πŸ™Œ

Upcoming Blogs

  1. Understanding React Limitations

  2. JavaScript Fundamentals for React

  3. Comment topics that you would like to cover first I will give them priority.

✍️ Please let me know in the comments if you prefer the current length of the blog or if you would like it to be shorter. Your feedback is valuable, and I want to ensure that the blog meets your expectations in terms of content length and readability. Thank you! 😊

Β