The Main Concepts
- Components:
- React applications are built using components, which are independent and reusable pieces of UI.
- Components can be functional (written as JavaScript functions) or class-based (written as ES6 classes).
- Each component has its own state and props.
- JSX (JavaScript XML):
- JSX is a syntax extension for JavaScript, used in React to describe what the UI should look like.
- It looks similar to HTML but allows embedding JavaScript expressions within curly braces (
{}
).
- State and Lifecycle:
- State is an object that holds some information that may change over the lifetime of the component.
- React components can have lifecycle methods (in class components) that allow executing code at specific times in the component's life (mounting, updating, unmounting).
- Props (Properties):
- Props are how components talk to each other.
- They are read-only and allow passing data from a parent component to a child component.
- Hooks:
- Hooks are functions that let you use state and other React features in functional components.
- Common hooks include
useState
, useEffect
, and useContext
.
- Virtual DOM and Reconciliation:
- React creates a virtual representation of the UI in memory (the virtual DOM).
- When the state of an object changes, React first changes the object in the virtual DOM. Then, using a reconciliation algorithm, it updates the real DOM to match the virtual DOM in an efficient way.
- Event Handling:
- React elements handle events similarly to HTML elements but use camelCase syntax.
- Event handlers in React are written as functions and passed to elements as props.
- Conditional Rendering:
- React allows rendering different UI markup based on certain conditions, much like JavaScript's conditional statements.
- Lists and Keys:
- React uses the
key
prop to create a relationship between the component and the DOM element in lists, improving performance during updates.
- Composition vs Inheritance:
- React has a powerful composition model and recommends using composition instead of inheritance to reuse code between components.
- Immutability of State:
- React expects that the state is immutable and should not be changed directly. Instead, use setState or the useState hook.
- Higher-Order Components:
- A higher-order component is a function that takes a component and returns a new component, allowing you to reuse component logic.
- Context API:
- The Context API allows you to share values (like state) across the component tree without having to pass props down manually at every level.
Domain Object Model (DOM)
A programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as a tree of nodes and objects; this way, programming languages can interact with the page.
Key Aspects of the DOM:
- Tree Structure: The DOM organizes a web page as a tree of nodes, including elements, text, and attributes. Each node in this tree corresponds to a part of the document (e.g., an element, an attribute of an element, or a piece of text).
- Language-Independent: While the DOM is most commonly used with JavaScript, it's designed to be language-independent. This means it can be accessed and manipulated by various programming languages.