Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

React FAQ: Top 75 Questions

1. What is React?

React (also known as React.js or ReactJS) is a free and open-source front-end JavaScript library for building user interfaces based on UI components. It is maintained by Meta (formerly Facebook) and a community of individual developers and companies.

Key characteristics of React include:

  • Component-Based: React applications are built using isolated, reusable pieces of UI called components. Each component manages its own state and renders itself independently.
  • Declarative: You describe the desired state of your UI, and React figures out how to render it. This makes your code more predictable and easier to debug.
  • Virtual DOM: React uses a Virtual DOM (VDOM) to optimize rendering performance. Instead of directly manipulating the browser's DOM, React first updates its in-memory VDOM, then efficiently calculates the differences (diffing), and finally updates only the necessary parts of the real DOM.
  • JSX: React uses JSX (JavaScript XML), a syntax extension that allows you to write HTML-like code directly within your JavaScript files. This makes it easier to describe what your UI should look like.
  • Unidirectional Data Flow (One-Way Data Binding): Data in React flows in a single direction, from parent components to child components via props. This makes it easier to understand how data changes affect your application.
  • "Learn Once, Write Anywhere": React is not a framework but a library. It can be used for web development, but with React Native, it can also be used for mobile app development (iOS and Android), and even for desktop applications with Electron.

import React from 'react';
import ReactDOM from 'react-dom/client';

// Functional Component Example
function WelcomeMessage(props) {
  return <h1>Hello, {props.name}!</h1>;
}

// Class Component Example (older style, but still valid)
class Greeting extends React.Component {
  render() {
    return <p>Nice to meet you, {this.props.person}.</p>;
  }
}

// App Component using both
function App() {
  return (
    <div>
      <WelcomeMessage name="React User" />
      <Greeting person="Developer" />
      <p>This is a simple React application demonstrating components and JSX.</p>
    </div>
  );
}

// Rendering the App component into the DOM
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
                    

Explanation of the Code:

  • The code starts by importing `React` (for JSX and component creation) and `ReactDOM` (for rendering React components into the browser's DOM).
  • `WelcomeMessage` is a **functional component** that receives `props` (properties) and renders an `h1` tag with a personalized greeting using **JSX**.
  • `Greeting` is a **class component** demonstrating the older way of defining components, also using `this.props` for data.
  • `App` is another functional component that acts as the main container, rendering instances of `WelcomeMessage` and `Greeting`. This showcases **component reusability** and how components are composed.
  • Finally, `ReactDOM.createRoot().render()` is used to mount the `App` component onto the HTML element with the `id='root'`, making the React UI visible in the browser.

This example highlights React's core principles: component-based architecture, declarative UI, and the use of JSX.