React (also known as React.js or ReactJS) is a free and open-source front-end JavaScript library[5][6] that aims to make building user interfaces based on components more "seamless".[5] It is maintained by Meta (formerly Facebook) and a community of individual developers and companies.[7][8][9]
React can be used to develop single-page, mobile, or server-rendered applications with frameworks like Next.js and Remix[a]. Because React is only concerned with the user interface and rendering components to the DOM, React applications often rely on libraries for routing and other client-side functionality.[11][12] A key advantage of React is that it only re-renders those parts of the page that have changed, avoiding unnecessary re-rendering of unchanged DOM elements.
Notable features
Declarative
React adheres to the declarative programming paradigm.[13][14]: 76  Developers design views for each state of an application, and React updates and renders components when data changes. This is in contrast with imperative programming.[15]
Components
React code is made of entities called components.[14]: 10–12  These components are modular and can be reused.[14]: 70  React applications typically consist of many layers of components. The components are rendered to a root element in the DOM using the React DOM library. When rendering a component, values are passed between components through props (short for "properties"). Values internal to a component are called its state.[16]
The two primary ways of declaring components in React are through function components and class components.[14]: 118 [17]: 10  Since React v16.8, using function components is the recommended way.
Function components
Function components, announced at React Conf 2018, and available since React v16.8, are declared with a function that accepts a single "props" argument and returns JSX. Function components can use internal state with the useState Hook. [18]
React Hooks
On February 16, 2019, React 16.8 was released to the public, introducing React Hooks.[18] Hooks are functions that let developers "hook into" React state and lifecycle features from function components.[19] Notably, Hooks do not work inside classes — they let developers use more features of React without classes.[20]
React provides several built-in hooks such as useState,[21][17]: 37  useContext,[14]: 11 [22][17]: 12  useReducer,[14]: 92 [22][17]: 65–66  useMemo[14]: 154 [22][17]: 162  and useEffect.[23][17]: 93–95  Others are documented in the Hooks API Reference.[24][14]: 62  useState and useEffect, which are the most commonly used, are for controlling state[14]: 37  and side effects,[14]: 61  respectively.
Rules of hooks
There are two rules of hooks[25]Â which describe the characteristic code patterns that hooks rely on:
- "Only call hooks at the top level" — do not call hooks from inside loops, conditions, or nested statements so that the hooks are called in the same order each render.
- "Only call hooks from React functions" — do not call hooks from plain JavaScript functions so that stateful logic stays with the component.
Although these rules cannot be enforced at runtime, code analysis tools such as linters can be configured to detect many mistakes during development. The rules apply to both usage of Hooks and the implementation of custom Hooks,[26] which may call other Hooks.
Server components
React server components (RSC) [27] are function components that run exclusively on the server. The concept was first introduced in the talk "Data Fetching with Server Components".[28] Though a similar concept to Server Side Rendering, RSCs do not send corresponding JavaScript to the client as no hydration occurs. As a result, they have no access to hooks. However, they may be asynchronous function, allowing them to directly perform asynchronous operations:
async function MyComponent() {
const message = await fetchMessageFromDb();
return (
<div>Message: {message}</div>
);
}
Currently, server components are most readily usable with Next.js. With Next.js, it's possible to write components for both the server and the client (browser). When a server rendered component is received by the browser, React in the browser takes over and creates the virtual DOM and attach event handlers. This is called hydration. [29]
Class components
Class components are declared using ES6 classes. They behave the same way that function components do, but instead of using Hooks to manage state and lifecycle events, they use the lifecycle methods on the React.Component base class.
class ParentComponent extends React.Component {
state = { color: 'green' };
render() {
return (
<ChildComponent color={this.state.color} />
);
}
}
The introduction of React Hooks with React 16.8 in February 2019 allowed developers to manage state and lifecycle behaviors within functional components, reducing the reliance on class components.
This trend aligns with the broader industry movement towards functional programming and modular design. As React continues to evolve, it is essential for developers to consider the benefits of functional components and React Hooks when building new applications or refactoring existing ones.[30]
Routing
React itself does not come with built-in support for routing. React is primarily a library for building user interfaces, and it does not include a full-fledged routing solution out of the box. Third-party libraries can be used to handle routing in React applications.[31] It allows the developer to define routes, manage navigation, and handle URL changes in a React-friendly way.
There is a Virtual DOM that is used to implement the real DOM
Virtual DOM
Another notable feature is the use of a virtual Document Object Model, or Virtual DOM. React creates an in-memory data-structure, similar to the browser DOM. Every time components are rendered, the result is compared with the virtual DOM. It then updates the browser's displayed DOM efficiently with only the computed differences.[32] This process is called reconciliation. This allows the programmer to write code as if the entire page is rendered on each change, while React only renders the components that actually change. This selective rendering provides a major performance boost.[33][34]
