= | { [K in Tag]: P extends JSX.IntrinsicElements[K] ? K : never }[Tag] | ComponentType
; /** * Represents any user-defined component, either as a function or a class. * * Similar to {@link JSXElementConstructor}, but with extra properties like * {@link FunctionComponent.defaultProps defaultProps }. * * @template P The props the component accepts. * * @see {@link ComponentClass} * @see {@link FunctionComponent} */ type ComponentType
= ComponentClass
| FunctionComponent
; /** * Represents any user-defined component, either as a function or a class. * * Similar to {@link ComponentType}, but without extra properties like * {@link FunctionComponent.defaultProps defaultProps }. * * @template P The props the component accepts. */ type JSXElementConstructor
=
| ((
props: P,
) => ReactNode)
// constructor signature must match React.Component
| (new(props: P) => Component > {}
/**
* @deprecated Use `ReactElement >`
*/
interface FunctionComponentElement extends ReactElement > {
/**
* @deprecated Use `element.props.ref` instead.
*/
ref?: ("ref" extends keyof P ? P extends { ref?: infer R | undefined } ? R : never : never) | undefined;
}
/**
* @deprecated Use `ReactElement >`
*/
type CElement > = ComponentElement ;
/**
* @deprecated Use `ReactElement >`
*/
interface ComponentElement > extends ReactElement > {
/**
* @deprecated Use `element.props.ref` instead.
*/
ref?: Ref = CElement >;
// string fallback for custom web-components
/**
* @deprecated Use `ReactElement `
*/
interface DOMElement | SVGAttributes
{
/**
* @deprecated Use `element.props.ref` instead.
*/
ref: Ref , T extends HTMLElement> extends DOMElement {
type: HTMLElementType;
}
// ReactSVG for ReactSVGElement
interface ReactSVGElement extends DOMElement , T extends HTMLElement>(
type: HTMLElementType,
props?: ClassAttributes ;
function createElement , T extends SVGElement>(
type: SVGElementType,
props?: ClassAttributes , T extends Element>(
type: string,
props?: ClassAttributes ;
// Custom components
function createElement (
type: FunctionComponent ,
props?: Attributes & P | null,
...children: ReactNode[]
): FunctionComponentElement ;
function createElement , C extends ComponentClass >(
type: ClassType ,
props?: ClassAttributes ;
function createElement (
type: FunctionComponent | ComponentClass | string,
props?: Attributes & P | null,
...children: ReactNode[]
): ReactElement ;
// DOM Elements
// ReactHTMLElement
function cloneElement , T extends HTMLElement>(
element: DetailedReactHTMLElement ,
props?: P,
...children: ReactNode[]
): DetailedReactHTMLElement ;
// ReactHTMLElement, less specific
function cloneElement , T extends HTMLElement>(
element: ReactHTMLElement , T extends SVGElement>(
element: ReactSVGElement,
props?: P,
...children: ReactNode[]
): ReactSVGElement;
// DOM Element (has to be the last, because type checking stops at first overload that fits)
function cloneElement , T extends Element>(
element: DOMElement ,
props?: DOMAttributes ;
// Custom components
function cloneElement (
element: FunctionComponentElement ,
props?: Partial & Attributes,
...children: ReactNode[]
): FunctionComponentElement ;
function cloneElement >(
element: CElement ,
props?: Partial & ClassAttributes ;
function cloneElement (
element: ReactElement ,
props?: Partial & Attributes,
...children: ReactNode[]
): ReactElement ;
/**
* Describes the props accepted by a Context {@link Provider}.
*
* @template T The type of the value the context provides.
*/
interface ProviderProps {
(props: P): ReactNode;
readonly $$typeof: symbol;
}
/**
* An {@link ExoticComponent} with a `displayName` property applied to it.
*
* @template P The props the component accepts.
*/
interface NamedExoticComponent extends ExoticComponent {
/**
* Used in debugging messages. You might want to set it
* explicitly if you want to display a different name for
* debugging purposes.
*
* @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
*/
displayName?: string | undefined;
}
/**
* An {@link ExoticComponent} with a `propTypes` property applied to it.
*
* @template P The props the component accepts.
*/
interface ProviderExoticComponent extends ExoticComponent {
}
/**
* Used to retrieve the type of a context object from a {@link Context}.
*
* @template C The context object.
*
* @example
*
* ```tsx
* import { createContext } from 'react';
*
* const MyContext = createContext({ foo: 'bar' });
*
* type ContextType = ContextType (object: {} | null | undefined): object is ReactElement ;
const Children: {
map extends ComponentLifecycle {}
class Component {
/**
* If set, `this.context` will be set at runtime to the current value of the given Context.
*
* @example
*
* ```ts
* type MyContext = number
* const Ctx = React.createContext ) => Pick ;
state: Readonly extends Component {}
/**
* @deprecated Use `ClassicComponent` from `create-react-class`
*
* @see {@link https://legacy.reactjs.org/docs/react-without-es6.html Legacy React Docs}
* @see {@link https://www.npmjs.com/package/create-react-class `create-react-class` on npm}
*/
interface ClassicComponent extends Component {
replaceState(nextState: S, callback?: () => void): void;
isMounted(): boolean;
getInitialState?(): S;
}
//
// Class Interfaces
// ----------------------------------------------------------------------
/**
* Represents the type of a function component. Can optionally
* receive a type argument that represents the props the component
* receives.
*
* @template P The props the component accepts.
* @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/function_components React TypeScript Cheatsheet}
* @alias for {@link FunctionComponent}
*
* @example
*
* ```tsx
* // With props:
* type Props = { name: string }
*
* const MyComponent: FC = FunctionComponent ;
/**
* Represents the type of a function component. Can optionally
* receive a type argument that represents the props the component
* accepts.
*
* @template P The props the component accepts.
* @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/function_components React TypeScript Cheatsheet}
*
* @example
*
* ```tsx
* // With props:
* type Props = { name: string }
*
* const MyComponent: FunctionComponent {
(props: P): ReactNode;
/**
* Ignored by React.
* @deprecated Only kept in types for backwards compatibility. Will be removed in a future major release.
*/
propTypes?: any;
/**
* Used in debugging messages. You might want to set it
* explicitly if you want to display a different name for
* debugging purposes.
*
* @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
*
* @example
*
* ```tsx
*
* const MyComponent: FC = () => {
* return extends StaticLifecycle {
// constructor signature must match React.Component
new(props: P): Component ;
/**
* Ignored by React.
* @deprecated Only kept in types for backwards compatibility. Will be removed in a future major release.
*/
propTypes?: any;
contextType?: Context | undefined;
/**
* Used in debugging messages. You might want to set it
* explicitly if you want to display a different name for
* debugging purposes.
*
* @see {@link https://legacy.reactjs.org/docs/react-component.html#displayname Legacy React Docs}
*/
displayName?: string | undefined;
}
/**
* @deprecated Use `ClassicComponentClass` from `create-react-class`
*
* @see {@link https://legacy.reactjs.org/docs/react-without-es6.html Legacy React Docs}
* @see {@link https://www.npmjs.com/package/create-react-class `create-react-class` on npm}
*/
interface ClassicComponentClass extends ComponentClass {
new(props: P): ClassicComponent ;
getDefaultProps?(): P;
}
/**
* Used in {@link createElement} and {@link createFactory} to represent
* a class.
*
* An intersection type is used to infer multiple type parameters from
* a single argument, which is useful for many top-level API defs.
* See {@link https://github.com/Microsoft/TypeScript/issues/7234 this GitHub issue}
* for more info.
*/
type ClassType , C extends ComponentClass > =
& C
& (new(props: P) => T);
//
// Component Specs and Lifecycle
// ----------------------------------------------------------------------
// This should actually be something like `Lifecycle | DeprecatedLifecycle `,
// as React will _not_ call the deprecated lifecycle methods if any of the new lifecycle
// methods are present.
interface ComponentLifecycle extends NewLifecycle , DeprecatedLifecycle {
/**
* Called immediately after a component is mounted. Setting state here will trigger re-rendering.
*/
componentDidMount?(): void;
/**
* Called to determine whether the change in props and state should trigger a re-render.
*
* `Component` always returns true.
* `PureComponent` implements a shallow comparison on props and state and returns true if any
* props or states have changed.
*
* If false is returned, {@link Component.render}, `componentWillUpdate`
* and `componentDidUpdate` will not be called.
*/
shouldComponentUpdate?(nextProps: Readonly , nextState: Readonly {
getDerivedStateFromProps?: GetDerivedStateFromProps | undefined;
getDerivedStateFromError?: GetDerivedStateFromError | undefined;
}
type GetDerivedStateFromProps =
/**
* Returns an update to a component's state based on its new props and old state.
*
* Note: its presence prevents any of the deprecated lifecycle methods from being invoked
*/
(nextProps: Readonly , prevState: S) => Partial =
/**
* This lifecycle is invoked after an error has been thrown by a descendant component.
* It receives the error that was thrown as a parameter and should return a value to update state.
*
* Note: its presence prevents any of the deprecated lifecycle methods from being invoked
*/
(error: any) => Partial {
/**
* Runs before React applies the result of {@link Component.render render} to the document, and
* returns an object to be given to {@link componentDidUpdate}. Useful for saving
* things such as scroll position before {@link Component.render render} causes changes to it.
*
* Note: the presence of this method prevents any of the deprecated
* lifecycle events from running.
*/
getSnapshotBeforeUpdate?(prevProps: Readonly , prevState: Readonly , prevState: Readonly {
/**
* Called immediately before mounting occurs, and before {@link Component.render}.
* Avoid introducing any side-effects or subscriptions in this method.
*
* Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
* or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
* this from being invoked.
*
* @deprecated 16.3, use {@link ComponentLifecycle.componentDidMount componentDidMount} or the constructor instead; will stop working in React 17
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state}
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
*/
componentWillMount?(): void;
/**
* Called immediately before mounting occurs, and before {@link Component.render}.
* Avoid introducing any side-effects or subscriptions in this method.
*
* This method will not stop working in React 17.
*
* Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
* or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
* this from being invoked.
*
* @deprecated 16.3, use {@link ComponentLifecycle.componentDidMount componentDidMount} or the constructor instead
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state}
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
*/
UNSAFE_componentWillMount?(): void;
/**
* Called when the component may be receiving new props.
* React may call this even if props have not changed, so be sure to compare new and existing
* props if you only want to handle changes.
*
* Calling {@link Component.setState} generally does not trigger this method.
*
* Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
* or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
* this from being invoked.
*
* @deprecated 16.3, use static {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} instead; will stop working in React 17
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
*/
componentWillReceiveProps?(nextProps: Readonly ): void;
/**
* Called when the component may be receiving new props.
* React may call this even if props have not changed, so be sure to compare new and existing
* props if you only want to handle changes.
*
* Calling {@link Component.setState} generally does not trigger this method.
*
* This method will not stop working in React 17.
*
* Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
* or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
* this from being invoked.
*
* @deprecated 16.3, use static {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} instead
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
*/
UNSAFE_componentWillReceiveProps?(nextProps: Readonly ): void;
/**
* Called immediately before rendering when new props or state is received. Not called for the initial render.
*
* Note: You cannot call {@link Component.setState} here.
*
* Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
* or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
* this from being invoked.
*
* @deprecated 16.3, use getSnapshotBeforeUpdate instead; will stop working in React 17
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
*/
componentWillUpdate?(nextProps: Readonly , nextState: Readonly , nextState: Readonly extends NamedExoticComponent {
/**
* Ignored by React.
* @deprecated Only kept in types for backwards compatibility. Will be removed in a future major release.
*/
propTypes?: any;
}
/**
* Lets your component expose a DOM node to a parent component
* using a ref.
*
* @see {@link https://react.dev/reference/react/forwardRef React Docs}
* @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/forward_and_create_ref/ React TypeScript Cheatsheet}
*
* @param render See the {@link ForwardRefRenderFunction}.
*
* @template T The type of the DOM node.
* @template P The props the component accepts, if any.
*
* @example
*
* ```tsx
* interface Props {
* children?: ReactNode;
* type: "submit" | "button";
* }
*
* export const FancyButton = forwardRef = P & { children?: ReactNode | undefined };
/**
* Used to retrieve the props a component accepts. Can either be passed a string,
* indicating a DOM element (e.g. 'div', 'span', etc.) or the type of a React
* component.
*
* It's usually better to use {@link ComponentPropsWithRef} or {@link ComponentPropsWithoutRef}
* instead of this type, as they let you be explicit about whether or not to include
* the `ref` prop.
*
* @see {@link https://react-typescript-cheatsheet.netlify.app/docs/react-types/componentprops/ React TypeScript Cheatsheet}
*
* @example
*
* ```tsx
* // Retrieves the props an 'input' element accepts
* type InputProps = React.ComponentProps<'input'>;
* ```
*
* @example
*
* ```tsx
* const MyComponent = (props: { foo: number, bar: string }) => (
Component: FunctionComponent ,
propsAreEqual?: (prevProps: Readonly , nextProps: Readonly ) => boolean,
): NamedExoticComponent ;
function memoHello
* World
* Hello
* World
* >
* ```
*/
const Fragment: ExoticComponent<{ children?: ReactNode | undefined }>;
/**
* Lets you find common bugs in your components early during development.
*
* @see {@link https://react.dev/reference/react/StrictMode React Docs}
*
* @example
*
* ```tsx
* import { StrictMode } from 'react';
*
* , props: Readonly | S | null) | (Pick | S | null),
callback?: () => void,
): void;
forceUpdate(callback?: () => void): void;
render(): ReactNode;
readonly props: Readonly;
}
class PureComponent): boolean;
/**
* Called immediately before a component is destroyed. Perform any necessary cleanup in this method, such as
* cancelled network requests, or cleaning up any DOM elements created in `componentDidMount`.
*/
componentWillUnmount?(): void;
/**
* Catches exceptions generated in descendant components. Unhandled exceptions will cause
* the entire component tree to unmount.
*/
componentDidCatch?(error: Error, errorInfo: ErrorInfo): void;
}
// Unfortunately, we have no way of declaring that the component constructor must implement this
interface StaticLifecycle | null;
type GetDerivedStateFromError | null;
// This should be "infer SS" but can't use it yet
interface NewLifecycle): SS | null;
/**
* Called immediately after updating occurs. Not called for the initial render.
*
* The snapshot is only present if {@link getSnapshotBeforeUpdate} is present and returns non-null.
*/
componentDidUpdate?(prevProps: Readonly, snapshot?: SS): void;
}
interface DeprecatedLifecycle): void;
/**
* Called immediately before rendering when new props or state is received. Not called for the initial render.
*
* Note: You cannot call {@link Component.setState} here.
*
* This method will not stop working in React 17.
*
* Note: the presence of {@link NewLifecycle.getSnapshotBeforeUpdate getSnapshotBeforeUpdate}
* or {@link StaticLifecycle.getDerivedStateFromProps getDerivedStateFromProps} prevents
* this from being invoked.
*
* @deprecated 16.3, use getSnapshotBeforeUpdate instead
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
*/
UNSAFE_componentWillUpdate?(nextProps: Readonly): void;
}
function createRef = S | ((prevState: S) => S);
/**
* A function that can be used to update the state of a {@link useState}
* or {@link useReducer} hook.
*/
type Dispatch = (value: A) => void;
/**
* A {@link Dispatch} function can sometimes be called without any arguments.
*/
type DispatchWithoutAction = () => void;
// Limit the reducer to accept only 0 or 1 action arguments
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
type AnyActionArg = [] | [any];
// Get the dispatch type from the reducer arguments (captures optional action argument correctly)
type ActionDispatch = (prevState: S, action: A) => S;
// If useReducer accepts a reducer without action, dispatch may be called without any parameters.
type ReducerWithoutAction = (prevState: S) => S;
// types used to try and prevent the compiler from reducing S
// to a supertype common with the second argument to useReducer()
type ReducerState(initialState: S | (() => S)): [S, Dispatch(): [S | undefined, Dispatch(
reducer: (prevState: S, ...args: A) => S,
initialState: S,
): [S, ActionDispatch];
/**
* An alternative to `useState`.
*
* `useReducer` is usually preferable to `useState` when you have complex state logic that involves
* multiple sub-values. It also lets you optimize performance for components that trigger deep
* updates because you can pass `dispatch` down instead of callbacks.
*
* @version 16.8.0
* @see {@link https://react.dev/reference/react/useReducer}
*/
function useReducer(
reducer: (prevState: S, ...args: A) => S,
initialState: S,
): [S, ActionDispatch];
/**
* An alternative to `useState`.
*
* `useReducer` is usually preferable to `useState` when you have complex state logic that involves
* multiple sub-values. It also lets you optimize performance for components that trigger deep
* updates because you can pass `dispatch` down instead of callbacks.
*
* @version 16.8.0
* @see {@link https://react.dev/reference/react/useReducer}
*/
function useReducer(
reducer: (prevState: S, ...args: A) => S,
initialArg: I,
init: (i: I) => S,
): [S, ActionDispatch];
/**
* `useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
* (`initialValue`). The returned object will persist for the full lifetime of the component.
*
* Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
* value around similar to how you’d use instance fields in classes.
*
* @version 16.8.0
* @see {@link https://react.dev/reference/react/useRef}
*/
function useRef