> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# Class: Component\<P = \{ }, S = \{ }, SS = `any`>

## Extends

- `ComponentLifecycle`\<`P`, `S`, `SS`>

## Extended by

- [`PureComponent`](/next/zh/api/react/Components/PureComponent.md)

## Type Parameters

| Type Parameter | Default type |
| -------------- | ------------ |
| `P`            | `object`     |
| `S`            | `object`     |
| `SS`           | `any`        |

## Constructors

### Constructor

> **new Component**\<`P` = \{ }, `S` = \{ }, `SS` = `any`>(`props`: `P`): `Component`\<`P`, `S`, `SS`>

#### Parameters

| Parameter | Type |
| --------- | ---- |
| `props`   | `P`  |

#### Returns

`Component`\<`P`, `S`, `SS`>

#### Inherited from

`ComponentLifecycle<P, S, SS>.constructor`

### Constructor

> **new Component**\<`P` = \{ }, `S` = \{ }, `SS` = `any`>(`props`: `P`, `context`: `any`): `Component`\<`P`, `S`, `SS`>

#### Parameters

| Parameter | Type  | Description                                                                            |
| --------- | ----- | -------------------------------------------------------------------------------------- |
| `props`   | `P`   | -                                                                                      |
| `context` | `any` | `contextType` 指定的父级 [Context](https://react.dev/reference/react/Component#context) 的值。 |

#### Returns

`Component`\<`P`, `S`, `SS`>

#### Inherited from

`ComponentLifecycle<P, S, SS>.constructor`

## Properties

### context

> **context**: `unknown`

如果使用 React Context，请在你的类中重新声明它，
使其成为 `static contextType` 的 `React.ContextType`。
应与类型标注或 static contextType 配合使用。

#### Example

```ts
static contextType = MyContext
// For TS pre-3.7:
context!: React.ContextType<typeof MyContext>
// For TS 3.7 and above:
declare context: React.ContextType<typeof MyContext>
```

#### See

[React Docs](https://react.dev/reference/react/Component#context)

***

### props

> `readonly` **props**: `Readonly`\<`P`>

***

### state

> **state**: `Readonly`\<`S`>

***

### contextType?

> `static` `optional` **contextType?**: `Context`\<`any`>

如果设置了该项，`this.context` 会在运行时被设置为给定 Context 的当前值。

#### Example

```ts
type MyContext = number
const Ctx = React.createContext<MyContext>(0)

class Foo extends React.Component {
  static contextType = Ctx
  context!: React.ContextType<typeof Ctx>
  render () {
    return <>My context's value: {this.context}</>;
  }
}
```

#### See

[https://react.dev/reference/react/Component#static-contexttype](https://react.dev/reference/react/Component#static-contexttype)

***

### ~~propTypes?~~

> `static` `optional` **propTypes?**: `any`

React 会忽略该项。

#### Deprecated

仅为向后兼容而保留在类型中。将在未来的主版本中移除。

## Methods

### componentDidCatch()?

> `optional` **componentDidCatch**(`error`: `Error`, `errorInfo`: `ErrorInfo`): `void`

捕获后代组件中产生的异常。未处理的异常会导致
整个组件树被卸载。

#### Parameters

| Parameter   | Type        |
| ----------- | ----------- |
| `error`     | `Error`     |
| `errorInfo` | `ErrorInfo` |

#### Returns

`void`

#### Inherited from

`ComponentLifecycle.componentDidCatch`

***

### componentDidMount()?

> `optional` **componentDidMount**(): `void`

在组件挂载后立即调用。在这里设置 state 会触发重新渲染。

#### Returns

`void`

#### Inherited from

`ComponentLifecycle.componentDidMount`

***

### componentDidUpdate()?

> `optional` **componentDidUpdate**(`prevProps`: `Readonly`\<`P`>, `prevState`: `Readonly`\<`S`>, `snapshot?`: `SS`): `void`

在更新发生后立即调用。首次渲染时不会调用。

只有当 [getSnapshotBeforeUpdate](#getsnapshotbeforeupdate) 存在且返回非 null 值时，该 snapshot 才会存在。

#### Parameters

| Parameter   | Type             |
| ----------- | ---------------- |
| `prevProps` | `Readonly`\<`P`> |
| `prevState` | `Readonly`\<`S`> |
| `snapshot?` | `SS`             |

#### Returns

`void`

#### Inherited from

`ComponentLifecycle.componentDidUpdate`

***

### ~~componentWillMount()?~~

> `optional` **componentWillMount**(): `void`

在挂载发生之前、以及 [Component.render](#render) 之前立即调用。
请避免在此方法中引入任何副作用或订阅。

注意：存在 [getSnapshotBeforeUpdate](#getsnapshotbeforeupdate)
或 [getDerivedStateFromProps](https://react.dev/reference/react/Component#static-getderivedstatefromprops) 时，
该方法不会被调用。

#### Returns

`void`

#### Deprecated

16.3 版本起，请改用 [componentDidMount](#componentdidmount) 或构造函数；在 React 17 中将失效

#### See

- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state)
- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path)

#### Inherited from

`ComponentLifecycle.componentWillMount`

***

### ~~componentWillReceiveProps()?~~

> `optional` **componentWillReceiveProps**(`nextProps`: `Readonly`\<`P`>, `nextContext`: `any`): `void`

在组件可能会接收到新的 props 时调用。
React 即使在 props 没有变化时也可能调用此方法，因此如果只想处理变化，
请务必比较新旧 props。

调用 [Component.setState](#setstate) 通常不会触发此方法。

注意：存在 [getSnapshotBeforeUpdate](#getsnapshotbeforeupdate)
或 [getDerivedStateFromProps](https://react.dev/reference/react/Component#static-getderivedstatefromprops) 时，
该方法不会被调用。

#### Parameters

| Parameter     | Type             |
| ------------- | ---------------- |
| `nextProps`   | `Readonly`\<`P`> |
| `nextContext` | `any`            |

#### Returns

`void`

#### Deprecated

16.3 版本起，请改用静态方法 [getDerivedStateFromProps](https://react.dev/reference/react/Component#static-getderivedstatefromprops)；在 React 17 中将失效

#### See

- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props)
- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path)

#### Inherited from

`ComponentLifecycle.componentWillReceiveProps`

***

### componentWillUnmount()?

> `optional` **componentWillUnmount**(): `void`

在组件被销毁之前立即调用。可以在此方法中执行必要的清理工作，例如
取消网络请求，或清理在 `componentDidMount` 中创建的 DOM 元素。

#### Returns

`void`

#### Inherited from

`ComponentLifecycle.componentWillUnmount`

***

### ~~componentWillUpdate()?~~

> `optional` **componentWillUpdate**(`nextProps`: `Readonly`\<`P`>, `nextState`: `Readonly`\<`S`>, `nextContext`: `any`): `void`

在接收到新的 props 或 state、即将渲染之前立即调用。首次渲染时不会调用。

注意：你不能在这里调用 [Component.setState](#setstate)。

注意：存在 [getSnapshotBeforeUpdate](#getsnapshotbeforeupdate)
或 [getDerivedStateFromProps](https://react.dev/reference/react/Component#static-getderivedstatefromprops) 时，
该方法不会被调用。

#### Parameters

| Parameter     | Type             |
| ------------- | ---------------- |
| `nextProps`   | `Readonly`\<`P`> |
| `nextState`   | `Readonly`\<`S`> |
| `nextContext` | `any`            |

#### Returns

`void`

#### Deprecated

16.3 版本起，请改用 getSnapshotBeforeUpdate；在 React 17 中将失效

#### See

- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update)
- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path)

#### Inherited from

`ComponentLifecycle.componentWillUpdate`

***

### forceUpdate()

> **forceUpdate**(`callback?`: () => `void`): `void`

#### Parameters

| Parameter   | Type         |
| ----------- | ------------ |
| `callback?` | () => `void` |

#### Returns

`void`

***

### getSnapshotBeforeUpdate()?

> `optional` **getSnapshotBeforeUpdate**(`prevProps`: `Readonly`\<`P`>, `prevState`: `Readonly`\<`S`>): `SS` | `null`

在 React 把 [render](#render) 的结果应用到文档之前运行，返回的对象会传给 [componentDidUpdate](#componentdidupdate)。适用于在 [render](#render) 引发变化之前，保存滚动位置等信息。

注意：存在该方法时，任何已废弃的
生命周期事件都不会执行。

#### Parameters

| Parameter   | Type             |
| ----------- | ---------------- |
| `prevProps` | `Readonly`\<`P`> |
| `prevState` | `Readonly`\<`S`> |

#### Returns

`SS` | `null`

#### Inherited from

`ComponentLifecycle.getSnapshotBeforeUpdate`

***

### render()

> **render**(): `ReactNode`

#### Returns

`ReactNode`

***

### setState()

> **setState**\<`K` _extends_ `string` | `number` | `symbol`>(`state`: `S` | ((`prevState`: `Readonly`\<`S`>, `props`: `Readonly`\<`P`>) => `S` | `Pick`\<`S`, `K`> | `null`) | `Pick`\<`S`, `K`> | `null`, `callback?`: () => `void`): `void`

#### Type Parameters

| Type Parameter                                 |
| ---------------------------------------------- |
| `K` _extends_ `string` \| `number` \| `symbol` |

#### Parameters

| Parameter   | Type                                                                                                                                     |
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `state`     | `S` \| ((`prevState`: `Readonly`\<`S`>, `props`: `Readonly`\<`P`>) => `S` \| `Pick`\<`S`, `K`> \| `null`) \| `Pick`\<`S`, `K`> \| `null` |
| `callback?` | () => `void`                                                                                                                             |

#### Returns

`void`

***

### shouldComponentUpdate()?

> `optional` **shouldComponentUpdate**(`nextProps`: `Readonly`\<`P`>, `nextState`: `Readonly`\<`S`>, `nextContext`: `any`): `boolean`

用于判断 props 和 state 的变化是否应该触发重新渲染。

`Component` 始终返回 true。
`PureComponent` 对 props 和 state 实现了浅比较，如果任何
props 或 state 发生变化，则返回 true。

如果返回 false，则不会调用 [Component.render](#render)、`componentWillUpdate`
和 `componentDidUpdate`。

#### Parameters

| Parameter     | Type             |
| ------------- | ---------------- |
| `nextProps`   | `Readonly`\<`P`> |
| `nextState`   | `Readonly`\<`S`> |
| `nextContext` | `any`            |

#### Returns

`boolean`

#### Inherited from

`ComponentLifecycle.shouldComponentUpdate`

***

### ~~UNSAFE_componentWillMount()?~~

> `optional` **UNSAFE_componentWillMount**(): `void`

在挂载发生之前、以及 [Component.render](#render) 之前立即调用。
请避免在此方法中引入任何副作用或订阅。

该方法在 React 17 中不会停止工作。

注意：存在 [getSnapshotBeforeUpdate](#getsnapshotbeforeupdate)
或 [getDerivedStateFromProps](https://react.dev/reference/react/Component#static-getderivedstatefromprops) 时，
该方法不会被调用。

#### Returns

`void`

#### Deprecated

16.3 版本起，请改用 [componentDidMount](#componentdidmount) 或构造函数

#### See

- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#initializing-state)
- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path)

#### Inherited from

`ComponentLifecycle.UNSAFE_componentWillMount`

***

### ~~UNSAFE_componentWillReceiveProps()?~~

> `optional` **UNSAFE_componentWillReceiveProps**(`nextProps`: `Readonly`\<`P`>, `nextContext`: `any`): `void`

在组件可能会接收到新的 props 时调用。
React 即使在 props 没有变化时也可能调用此方法，因此如果只想处理变化，
请务必比较新旧 props。

调用 [Component.setState](#setstate) 通常不会触发此方法。

该方法在 React 17 中不会停止工作。

注意：存在 [getSnapshotBeforeUpdate](#getsnapshotbeforeupdate)
或 [getDerivedStateFromProps](https://react.dev/reference/react/Component#static-getderivedstatefromprops) 时，
该方法不会被调用。

#### Parameters

| Parameter     | Type             |
| ------------- | ---------------- |
| `nextProps`   | `Readonly`\<`P`> |
| `nextContext` | `any`            |

#### Returns

`void`

#### Deprecated

16.3 版本起，请改用静态方法 [getDerivedStateFromProps](https://react.dev/reference/react/Component#static-getderivedstatefromprops)

#### See

- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props)
- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path)

#### Inherited from

`ComponentLifecycle.UNSAFE_componentWillReceiveProps`

***

### ~~UNSAFE_componentWillUpdate()?~~

> `optional` **UNSAFE_componentWillUpdate**(`nextProps`: `Readonly`\<`P`>, `nextState`: `Readonly`\<`S`>, `nextContext`: `any`): `void`

在接收到新的 props 或 state、即将渲染之前立即调用。首次渲染时不会调用。

注意：你不能在这里调用 [Component.setState](#setstate)。

该方法在 React 17 中不会停止工作。

注意：存在 [getSnapshotBeforeUpdate](#getsnapshotbeforeupdate)
或 [getDerivedStateFromProps](https://react.dev/reference/react/Component#static-getderivedstatefromprops) 时，
该方法不会被调用。

#### Parameters

| Parameter     | Type             |
| ------------- | ---------------- |
| `nextProps`   | `Readonly`\<`P`> |
| `nextState`   | `Readonly`\<`S`> |
| `nextContext` | `any`            |

#### Returns

`void`

#### Deprecated

16.3 版本起，请改用 getSnapshotBeforeUpdate

#### See

- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update)
- [https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path](https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path)

#### Inherited from

`ComponentLifecycle.UNSAFE_componentWillUpdate`
