You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/PureComponent.md
+17-17Lines changed: 17 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -4,13 +4,13 @@ title: PureComponent
4
4
5
5
<Pitfall>
6
6
7
-
We recommend defining components as functions instead of classes. [See how to migrate.](#alternatives)
7
+
我们建议使用函数式组件而非类式组件。[查看如何迁移](#alternatives)。
8
8
9
9
</Pitfall>
10
10
11
11
<Intro>
12
12
13
-
`PureComponent`is similar to [`Component`](/reference/react/Component) but it skips re-renders for same props and state. Class components are still supported by React, but we don't recommend using them in new code.
13
+
`PureComponent`类似于 [`Component`](/reference/react/Component),但是当 props 和 state 与之前保持一致时会跳过重新渲染。React 仍然支持类式组件,但我们不建议在新代码中使用。
14
14
15
15
```js
16
16
classGreetingextendsPureComponent {
@@ -26,11 +26,11 @@ class Greeting extends PureComponent {
26
26
27
27
---
28
28
29
-
## Reference {/*reference*/}
29
+
## 参考 {/*reference*/}
30
30
31
31
### `PureComponent` {/*purecomponent*/}
32
32
33
-
To skip re-rendering a class component for same props and state, extend`PureComponent`instead of [`Component`:](/reference/react/Component)
33
+
为了在 props 和 state 相同时跳过重新渲染,类式组件应该继承`PureComponent`而不是 [`Component`](/reference/react/Component):
34
34
35
35
```js
36
36
import { PureComponent } from'react';
@@ -42,18 +42,18 @@ class Greeting extends PureComponent {
42
42
}
43
43
```
44
44
45
-
`PureComponent`is a subclass of `Component`and supports [all the `Component`APIs.](/reference/react/Component#reference) Extending `PureComponent`is equivalent to defining a custom [`shouldComponentUpdate`](/reference/react/Component#shouldcomponentupdate)method that shallowly compares props and state.
React normally re-renders a component whenever its parent re-renders. As an optimization, you can create a component that React will not re-render when its parent re-renders so long as its new props and state are the same as the old props and state. [Class components](/reference/react/Component)can opt into this behavior by extending `PureComponent`:
56
+
当父组件重新渲染时,React 通常会重新渲染子组件。为了优化性能,你可以创建一个组件,在父组件重新渲染时不会重新渲染,前提是新的 props 和 state 与旧的 props 和 state 相同。[类式组件](/reference/react/Component)可以通过继承 `PureComponent` 来选择此行为。
57
57
58
58
```js {1}
59
59
classGreetingextendsPureComponent {
@@ -63,9 +63,9 @@ class Greeting extends PureComponent {
63
63
}
64
64
```
65
65
66
-
A React component should always have [pure rendering logic.](/learn/keeping-components-pure) This means that it must return the same output if its props, state, and context haven't changed. By using `PureComponent`, you are telling React that your component complies with this requirement, so React doesn't need to re-render as long as its props and state haven't changed. However, your component will still re-render if a context that it's using changes.
In this example, notice that the `Greeting`component re-renders whenever `name`is changed (because that's one of its props), but not when `address`is changed (because it's not passed to `Greeting` as a prop):
We recommend using function components instead of [class components](/reference/react/Component) in new code. If you have some existing class components using `PureComponent`, here is how you can convert them. This is the original code:
Unlike`PureComponent`, [`memo`](/reference/react/memo)does not compare the new and the old state. In function components, calling the [`set`function](/reference/react/useState#setstate) with the same state [already prevents re-renders by default,](/reference/react/memo#updating-a-memoized-component-using-state) even without `memo`.
206
+
与`PureComponent` 不同,[`memo`](/reference/react/memo)不会比较新旧 state。在函数组件中,即使没有 `memo`,调用具有相同 state 的 [`set`函数](/reference/react/useState#setstate),[默认已经阻止了重新渲染](/reference/react/memo#updating-a-memoized-component-using-state)。
0 commit comments