DailyDevDiet

logo - dailydevdiet

Learn. Build. Innovate. Elevate your coding skills with dailydevdiet!

Chapter 1: Introduction to React JS

Introduction to ReactJS

What is React?

React is a JavaScript library created by Facebook (now Meta) for building user interfaces, particularly single-page applications. Released as an open-source project in 2013, React has revolutionized front-end web development with its component-based architecture and efficient rendering system.

Key Characteristics of React

  • Component-Based: React applications are built using components—reusable, self-contained pieces of code that return HTML via a render function.
  • Declarative: You tell React what you want the UI to look like, and it handles the DOM updates for you.
  • Virtual DOM: React creates a lightweight representation of the real DOM in memory for performance optimization.
  • Unidirectional Data Flow: Data flows down from parent components to children, making code more predictable and easier to debug.
  • JSX: An HTML-like syntax extension that allows you to write HTML structures in JavaScript.

Why Choose React?

Advantages of React

  1. High Performance: React’s virtual DOM implementation minimizes costly DOM operations by batching updates.
  2. Reusability: Components can be reused throughout your application, saving development time.
  3. Strong Community Support: Backed by Facebook and used by countless developers worldwide.
  4. Large Ecosystem: Rich library of tools, extensions, and compatible packages.
  5. Developer Experience: Great developer tools and error messages make debugging easier.
  6. Mobile Development: Skills transfer to React Native for mobile app development.

React vs Other Frameworks

FeatureReactAngularVue
TypeLibraryFrameworkFramework
Learning CurveModerateSteepGentle
SizeLightweightComprehensiveLightweight
FlexibilityHighModerateHigh
Company BackingFacebook (Meta)GoogleCommunity

Core React Philosophy

React’s design philosophy centers around these key principles:

  1. Composition: Build complex UIs from simple, isolated components.
  2. Explicit Data Flow: Props pass data from parent to child in a clear, unidirectional flow.
  3. State Management: Components can maintain their own state to reflect UI changes.
  4. Virtual DOM: An abstraction of the DOM that enables React to update only what’s necessary.

A Brief History of React

  • 2011: React was first used internally at Facebook
  • 2012: Used on Instagram.com
  • 2013: Open-sourced at JSConf US
  • 2015: React Native was released
  • 2016: React 15 was released with improved rendering
  • 2017: React 16 (Fiber) brought complete rewrite of reconciliation algorithm
  • 2019: React 16.8 introduced Hooks
  • 2020: React 17 focused on making upgrades smoother
  • 2021: React 18 introduced concurrent rendering
  • 2022-2023: Further improvements to concurrent features, server components

Who Uses React?

React powers the user interfaces of many popular websites and applications, including:

  • Facebook
  • Instagram
  • Netflix
  • Airbnb
  • Discord
  • Dropbox
  • Reddit
  • Uber
  • WhatsApp Web
  • PayPal

Your First React Element

At its core, React is about creating elements that represent what you want to see on the screen. Here’s a simple example of a React element without JSX:

const element = React.createElement(
  'h1',
  {className: 'greeting'},
  'Hello, world!'
);

And the same example with JSX:

const element = <h1 className="greeting">Hello, world!</h1>;

Setting Up a Simple React Page

Here’s how to include React in an HTML page for simple experiments:

<!DOCTYPE html>
<html>
  <head>
    <title>My First React Page</title>
    <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
 </head>
  <body>
    <div id="root"></div>

    <script type="text/babel">
      function HelloWorld() {
        return <h1>Hello, React World!</h1>;
      }

      const container = document.getElementById('root');
      const root = ReactDOM.createRoot(container);
      root.render(<HelloWorld />);
    </script>

  </body>
</html>

The React Ecosystem

React itself is just the UI library, but a complete React application typically includes:

  • React DOM: For rendering React components in the browser
  • Babel: For transpiling JSX and modern JavaScript
  • Webpack/Vite: For bundling and building your application
  • React Router: For handling navigation and routing
  • State Management: Libraries like Redux, MobX, or React Context API

Styling Solutions: CSS-in-JS libraries, styled-components, etc.

Looking Ahead

As you progress through this book, you’ll learn how to:

  1. Set up a professional React development environment
  2. Build reusable components
  3. Manage component state
  4. Create multi-page applications with routing
  5. Handle forms and user input
  6. Fetch data from APIs
  7. Optimize performance
  8. Deploy your React application

Summary

React is a powerful JavaScript library for building user interfaces with a component-based approach. Its virtual DOM implementation and unidirectional data flow make it efficient and predictable. Created by Facebook in 2013, React has become one of the most popular frontend technologies, with a vibrant ecosystem and strong community support.

In the next chapter, we’ll set up your development environment and create your first React application using modern tools.

Further Reading

Scroll to Top