- Spec files
npm-react
Describes: pkg:npm/react@19.1.x
- Description
- React is a JavaScript library for building user interfaces with a declarative, component-based approach.
- Author
- tessl
- Last updated
index.md docs/
1# React23React is a JavaScript library for building user interfaces with a declarative, component-based approach. It enables developers to create interactive UIs by designing simple views for each application state, with React efficiently updating and rendering only the necessary components when data changes.45## Package Information67- **Package Name**: react8- **Package Type**: npm9- **Language**: JavaScript/TypeScript10- **Installation**: `npm install react`1112## Core Imports1314```typescript15import React from "react";16import { useState, useEffect, Component } from "react";17```1819For CommonJS:2021```javascript22const React = require("react");23const { useState, useEffect, Component } = require("react");24```2526### JSX Runtime Imports2728```typescript29// Automatic JSX runtime (React 17+)30// These are imported automatically by bundlers:31import { jsx, jsxs, Fragment } from "react/jsx-runtime";3233// Development JSX runtime34import { jsxDEV, Fragment } from "react/jsx-dev-runtime";3536// Manual JSX import (legacy)37import React from "react";38```3940## Basic Usage4142```typescript43import React, { useState } from "react";4445// Function component with hooks46function Counter() {47const [count, setCount] = useState(0);4849return (50<div>51<p>Count: {count}</p>52<button onClick={() => setCount(count + 1)}>53Increment54</button>55</div>56);57}5859// Class component60class Welcome extends React.Component {61render() {62return <h1>Hello, {this.props.name}!</h1>;63}64}6566// Creating elements67const element = React.createElement("h1", null, "Hello, world!");68```6970## Architecture7172React is built around several key concepts:7374- **Components**: Reusable UI pieces that accept props and return JSX elements75- **JSX**: Syntax extension for JavaScript that allows writing HTML-like code in components76- **Hooks**: Functions that let you use state and other React features in function components77- **Virtual DOM**: React's internal representation of the UI for efficient updates78- **Reconciliation**: The process by which React updates the DOM to match the virtual DOM79- **Context**: A way to pass data through the component tree without prop drilling8081## Capabilities8283### Components and Elements8485Core component classes and element creation functions for building React applications.8687```typescript { .api }88class Component<P = {}, S = {}> {89constructor(props: P);90setState<K extends keyof S>(91state: ((prevState: S, props: P) => (Pick<S, K> | S | null)) | (Pick<S, K> | S | null),92callback?: () => void93): void;94forceUpdate(callback?: () => void): void;95render(): React.ReactNode;96}9798class PureComponent<P = {}, S = {}> extends Component<P, S> {}99100function createElement<P extends {}>(101type: string | React.ComponentType<P>,102props?: P | null,103...children: React.ReactNode[]104): React.ReactElement<P>;105```106107[Components and Elements](./components.md)108109### Hooks110111React Hooks for state management, side effects, and component lifecycle in function components.112113```typescript { .api }114function useState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>];115116function useEffect(117effect: React.EffectCallback,118deps?: React.DependencyList119): void;120121function useContext<T>(context: React.Context<T>): T;122123function useReducer<R extends React.Reducer<any, any>>(124reducer: R,125initialState: React.ReducerState<R>,126initializer?: undefined127): [React.ReducerState<R>, React.Dispatch<React.ReducerAction<R>>];128129function useSyncExternalStore<Snapshot>(130subscribe: (onStoreChange: () => void) => () => void,131getSnapshot: () => Snapshot,132getServerSnapshot?: () => Snapshot133): Snapshot;134135function use<T>(usable: Promise<T> | React.Context<T>): T;136137function experimental_useEffectEvent<Args extends any[], Return>(138callback: (...args: Args) => Return139): (...args: Args) => Return;140```141142[Hooks](./hooks.md)143144### Context API145146React Context for sharing values between components without explicit prop passing.147148```typescript { .api }149function createContext<T>(defaultValue: T): React.Context<T>;150151interface Context<T> {152Provider: React.Provider<T>;153Consumer: React.Consumer<T>;154displayName?: string;155}156157interface Provider<T> {158value: T;159children?: React.ReactNode;160}161```162163[Context API](./context.md)164165### Refs and Forward Refs166167Reference system for accessing DOM nodes and component instances.168169```typescript { .api }170function createRef<T = any>(): React.RefObject<T>;171172function useRef<T = undefined>(): React.MutableRefObject<T | undefined>;173function useRef<T = undefined>(initialValue: T): React.MutableRefObject<T>;174175function forwardRef<T, P = {}>(176render: (props: P, ref: React.Ref<T>) => React.ReactElement | null177): React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<T>>;178179interface RefObject<T> {180readonly current: T | null;181}182183interface MutableRefObject<T> {184current: T;185}186```187188[Refs and Forward Refs](./refs.md)189190### Performance Optimization191192Tools for optimizing React application performance through memoization and lazy loading.193194```typescript { .api }195function memo<P extends object>(196Component: React.FunctionComponent<P>,197propsAreEqual?: (prevProps: P, nextProps: P) => boolean198): React.NamedExoticComponent<P>;199200function useMemo<T>(factory: () => T, deps: React.DependencyList | undefined): T;201202function useCallback<T extends (...args: any[]) => any>(203callback: T,204deps: React.DependencyList205): T;206207function lazy<T extends React.ComponentType<any>>(208factory: () => Promise<{ default: T }>209): React.LazyExoticComponent<T>;210```211212[Performance Optimization](./performance.md)213214### Concurrency and Transitions215216Concurrent features for managing non-urgent updates and improving user experience.217218```typescript { .api }219function useTransition(): [boolean, React.TransitionStartFunction];220221function startTransition(callback: () => void): void;222223function useDeferredValue<T>(value: T): T;224function useDeferredValue<T>(value: T, initialValue: T): T;225226function useOptimistic<S, A>(227state: S,228updateFn: (currentState: S, optimisticValue: A) => S229): [S, (optimisticValue: A) => void];230231// Experimental features232function unstable_addTransitionType(type: string): void;233function unstable_getCacheForType<T>(resourceType: () => T): T;234function unstable_useCacheRefresh(): <T>(?() => T, ?T) => void;235function unstable_useSwipeTransition<T>(236gesture: StartGesture<T>237): [boolean, (callback: () => void, options?: { gesture: T }) => void];238```239240[Concurrency and Transitions](./concurrency.md)241242### Suspense and Error Boundaries243244Components and utilities for handling async loading states and errors in React applications.245246```typescript { .api }247interface SuspenseProps {248children?: React.ReactNode;249fallback: React.ReactNode;250}251252class Suspense extends React.Component<SuspenseProps> {}253254class ErrorBoundary extends React.Component<255{ children: React.ReactNode; fallback?: React.ComponentType<any> },256{ hasError: boolean }257> {}258259// Experimental components260const unstable_SuspenseList: React.ExoticComponent<{261children: React.ReactNode;262revealOrder?: "forwards" | "backwards" | "together";263tail?: "collapsed" | "hidden";264}>;265266const unstable_LegacyHidden: React.ExoticComponent<{ children?: React.ReactNode }>;267const unstable_Activity: React.ExoticComponent<{ children?: React.ReactNode }>;268const unstable_Scope: React.ExoticComponent<{ children?: React.ReactNode }>;269const unstable_TracingMarker: React.ExoticComponent<{ children?: React.ReactNode }>;270const unstable_ViewTransition: React.ExoticComponent<{ children?: React.ReactNode }>;271272// Server-side functions273function unstable_postpone(reason: string): void;274```275276[Suspense and Error Boundaries](./suspense.md)277278### JSX Runtime279280JSX transformation functions for automatic JSX processing by bundlers and compilers.281282```typescript { .api }283// Production JSX runtime284function jsx(type: any, config: any, maybeKey?: string): React.ReactElement;285function jsxs(type: any, config: any, maybeKey?: string): React.ReactElement;286287// Development JSX runtime288function jsxDEV(289type: any,290config: any,291maybeKey?: string,292isStaticChildren?: boolean,293source?: any,294self?: any295): React.ReactElement;296297// Fragment symbol for both runtimes298const Fragment: React.ExoticComponent<{ children?: React.ReactNode }>;299```300301### Utilities302303Utility functions for working with React elements and development tools.304305```typescript { .api }306function isValidElement(object: any): object is React.ReactElement;307308function cloneElement<P>(309element: React.ReactElement<P>,310props?: Partial<P> & React.Attributes,311...children: React.ReactNode[]312): React.ReactElement<P>;313314const Children: {315map<T, C>(316children: C | readonly C[],317fn: (child: C, index: number) => T318): C extends null | undefined ? C : Array<Exclude<T, boolean | null | undefined>>;319forEach<C>(320children: C | readonly C[],321fn: (child: C, index: number) => void322): void;323count(children: any): number;324toArray(children: React.ReactNode | React.ReactNode[]): React.ReactElement[];325only<T>(children: T): T extends any[] ? never : T;326};327328// Server-side security functions329function taintUniqueValue(330message: string,331lifetime: any,332value: any333): void;334335function taintObjectReference(336message: string,337object: any338): void;339340// Development utilities341function act<T>(callback: () => T | Promise<T>): Promise<T>;342function captureOwnerStack(): string | null;343const version: string;344```345346[Utilities](./utilities.md)347348## Types349350### Core Types351352```typescript { .api }353type ReactNode = ReactElement | string | number | boolean | null | undefined | ReactNode[];354355interface ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> {356type: T;357props: P;358key: Key | null;359}360361type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;362363interface FunctionComponent<P = {}> {364(props: P, context?: any): ReactElement<any, any> | null;365propTypes?: WeakValidationMap<P>;366contextTypes?: ValidationMap<any>;367defaultProps?: Partial<P>;368displayName?: string;369}370371interface ComponentClass<P = {}, S = ComponentState> extends StaticLifecycle<P, S> {372new (props: P, context?: any): Component<P, S>;373propTypes?: WeakValidationMap<P>;374contextTypes?: ValidationMap<any>;375childContextTypes?: ValidationMap<any>;376defaultProps?: Partial<P>;377displayName?: string;378}379380type Key = string | number;381382type Ref<T> = RefCallback<T> | RefObject<T> | null;383384type RefCallback<T> = (instance: T | null) => void;385```