Skip to content

Commit be26f82

Browse files
mzkmnklacolaco
andauthored
docs: best-practices/runtime-performance/skipping-subtrees.mdの翻訳 (angular#1020)
* chore: rename `skipping-subtrees.md` `skipping-subtrees.en.md` * feat: `skipping-subtrees.md`の翻訳 * chore: 翻訳の修正 * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> * Update adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md Co-authored-by: Suguru Inatomi <[email protected]> --------- Co-authored-by: Suguru Inatomi <[email protected]>
1 parent cdab529 commit be26f82

File tree

2 files changed

+158
-26
lines changed

2 files changed

+158
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Skipping component subtrees
2+
3+
JavaScript, by default, uses mutable data structures that you can reference from multiple different components. Angular runs change detection over your entire component tree to make sure that the most up-to-date state of your data structures is reflected in the DOM.
4+
5+
Change detection is sufficiently fast for most applications. However, when an application has an especially large component tree, running change detection across the whole application can cause performance issues. You can address this by configuring change detection to only run on a subset of the component tree.
6+
7+
If you are confident that a part of the application is not affected by a state change, you can use [OnPush](/api/core/ChangeDetectionStrategy) to skip change detection in an entire component subtree.
8+
9+
## Using `OnPush`
10+
11+
OnPush change detection instructs Angular to run change detection for a component subtree **only** when:
12+
13+
* The root component of the subtree receives new inputs as the result of a template binding. Angular compares the current and past value of the input with `==`.
14+
* Angular handles an event _(for example using event binding, output binding, or `@HostListener` )_ in the subtree's root component or any of its children whether they are using OnPush change detection or not.
15+
16+
You can set the change detection strategy of a component to `OnPush` in the `@Component` decorator:
17+
18+
```ts
19+
import { ChangeDetectionStrategy, Component } from '@angular/core';
20+
@Component({
21+
changeDetection: ChangeDetectionStrategy.OnPush,
22+
})
23+
export class MyComponent {}
24+
```
25+
26+
## Common change detection scenarios
27+
28+
This section examines several common change detection scenarios to illustrate Angular's behavior.
29+
30+
### An event is handled by a component with default change detection
31+
32+
If Angular handles an event within a component without `OnPush` strategy, the framework executes change detection on the entire component tree. Angular will skip descendant component subtrees with roots using `OnPush`, which have not received new inputs.
33+
34+
As an example, if we set the change detection strategy of `MainComponent` to `OnPush` and the user interacts with a component outside the subtree with root `MainComponent`, Angular will check all the pink components from the diagram below (`AppComponent`, `HeaderComponent`, `SearchComponent`, `ButtonComponent`) unless `MainComponent` receives new inputs:
35+
36+
```mermaid
37+
graph TD;
38+
app[AppComponent] --- header[HeaderComponent];
39+
app --- main["MainComponent (OnPush)"];
40+
header --- search[SearchComponent];
41+
header --- button[ButtonComponent];
42+
main --- login["LoginComponent (OnPush)"];
43+
main --- details[DetailsComponent];
44+
event>Event] --- search
45+
46+
class app checkedNode
47+
class header checkedNode
48+
class button checkedNode
49+
class search checkedNode
50+
class event eventNode
51+
```
52+
53+
## An event is handled by a component with OnPush
54+
55+
If Angular handles an event within a component with OnPush strategy, the framework will execute change detection within the entire component tree. Angular will ignore component subtrees with roots using OnPush, which have not received new inputs and are outside the component which handled the event.
56+
57+
As an example, if Angular handles an event within `MainComponent`, the framework will run change detection in the entire component tree. Angular will ignore the subtree with root `LoginComponent` because it has `OnPush` and the event happened outside of its scope.
58+
59+
```mermaid
60+
graph TD;
61+
app[AppComponent] --- header[HeaderComponent];
62+
app --- main["MainComponent (OnPush)"];
63+
header --- search[SearchComponent];
64+
header --- button[ButtonComponent];
65+
main --- login["LoginComponent (OnPush)"];
66+
main --- details[DetailsComponent];
67+
event>Event] --- main
68+
69+
class app checkedNode
70+
class header checkedNode
71+
class button checkedNode
72+
class search checkedNode
73+
class main checkedNode
74+
class details checkedNode
75+
class event eventNode
76+
```
77+
78+
## An event is handled by a descendant of a component with OnPush
79+
80+
If Angular handles an event in a component with OnPush, the framework will execute change detection in the entire component tree, including the component’s ancestors.
81+
82+
As an example, in the diagram below, Angular handles an event in `LoginComponent` which uses OnPush. Angular will invoke change detection in the entire component subtree including `MainComponent` (`LoginComponent`’s parent), even though `MainComponent` has `OnPush` as well. Angular checks `MainComponent` as well because `LoginComponent` is part of its view.
83+
84+
```mermaid
85+
graph TD;
86+
app[AppComponent] --- header[HeaderComponent];
87+
app --- main["MainComponent (OnPush)"];
88+
header --- search[SearchComponent];
89+
header --- button[ButtonComponent];
90+
main --- login["LoginComponent (OnPush)"];
91+
main --- details[DetailsComponent];
92+
event>Event] --- login
93+
94+
class app checkedNode
95+
class header checkedNode
96+
class button checkedNode
97+
class search checkedNode
98+
class login checkedNode
99+
class main checkedNode
100+
class details checkedNode
101+
class event eventNode
102+
```
103+
104+
## New inputs to component with OnPush
105+
106+
Angular will run change detection within a child component with `OnPush` when setting an input property as result of a template binding.
107+
108+
For example, in the diagram below, `AppComponent` passes a new input to `MainComponent`, which has `OnPush`. Angular will run change detection in `MainComponent` but will not run change detection in `LoginComponent`, which also has `OnPush`, unless it receives new inputs as well.
109+
110+
```mermaid
111+
graph TD;
112+
app[AppComponent] --- header[HeaderComponent];
113+
app --- main["MainComponent (OnPush)"];
114+
header --- search[SearchComponent];
115+
header --- button[ButtonComponent];
116+
main --- login["LoginComponent (OnPush)"];
117+
main --- details[DetailsComponent];
118+
event>Parent passes new input to MainComponent]
119+
120+
class app checkedNode
121+
class header checkedNode
122+
class button checkedNode
123+
class search checkedNode
124+
class main checkedNode
125+
class details checkedNode
126+
class event eventNode
127+
```
128+
129+
## Edge cases
130+
131+
* **Modifying input properties in TypeScript code**. When you use an API like `@ViewChild` or `@ContentChild` to get a reference to a component in TypeScript and manually modify an `@Input` property, Angular will not automatically run change detection for OnPush components. If you need Angular to run change detection, you can inject `ChangeDetectorRef` in your component and call `changeDetectorRef.markForCheck()` to tell Angular to schedule a change detection.
132+
* **Modifying object references**. In case an input receives a mutable object as value and you modify the object but preserve the reference, Angular will not invoke change detection. That’s the expected behavior because the previous and the current value of the input point to the same reference.

adev-ja/src/content/best-practices/runtime-performance/skipping-subtrees.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# Skipping component subtrees
1+
# コンポーネントサブツリーをスキップする
22

3-
JavaScript, by default, uses mutable data structures that you can reference from multiple different components. Angular runs change detection over your entire component tree to make sure that the most up-to-date state of your data structures is reflected in the DOM.
3+
JavaScriptは、デフォルトでは、複数の異なるコンポーネントから参照できる可変データ構造を使用します。Angularは、データ構造の最新の状態がDOMに反映されるように、コンポーネントツリー全体で変更検知を実行します。
44

5-
Change detection is sufficiently fast for most applications. However, when an application has an especially large component tree, running change detection across the whole application can cause performance issues. You can address this by configuring change detection to only run on a subset of the component tree.
5+
変更検知は、ほとんどのアプリケーションにとって十分に高速です。ただし、アプリケーションが特に大きなコンポーネントツリーを持っている場合、アプリケーション全体で変更検知を実行すると、パフォーマンスの問題が発生する可能性があります。これは、コンポーネントツリーのサブセットでのみ変更検知が実行されるように構成することで対処できます。
66

7-
If you are confident that a part of the application is not affected by a state change, you can use [OnPush](/api/core/ChangeDetectionStrategy) to skip change detection in an entire component subtree.
7+
アプリケーションの一部が状態変化の影響を受けないと確信できる場合は、[OnPush](/api/core/ChangeDetectionStrategy)を使用して、コンポーネントのサブツリー全体の変更検知をスキップできます。
88

9-
## Using `OnPush`
9+
## `OnPush`の使用
1010

11-
OnPush change detection instructs Angular to run change detection for a component subtree **only** when:
11+
OnPush変更検知は、Angularにコンポーネントのサブツリーの変更検知を次の場合**のみ**実行するように指示します。
1212

13-
* The root component of the subtree receives new inputs as the result of a template binding. Angular compares the current and past value of the input with `==`.
14-
* Angular handles an event _(for example using event binding, output binding, or `@HostListener` )_ in the subtree's root component or any of its children whether they are using OnPush change detection or not.
13+
* サブツリーのルートコンポーネントが、テンプレートバインディングの結果として新しいインプットを受け取った場合。Angularは、インプットの現在と過去の値を`==`で比較します。
14+
* Angularが、OnPush変更検知を使用しているかどうかに関係なく、サブツリーのルートコンポーネント、または、その子でイベント *(例えば、イベントバインディング、アウトプットバインディング、または`@HostListener`を使用)* を処理する場合。
1515

16-
You can set the change detection strategy of a component to `OnPush` in the `@Component` decorator:
16+
コンポーネントの変更検知戦略を`@Component`デコレーターで`OnPush`に設定できます。
1717

1818
```ts
1919
import { ChangeDetectionStrategy, Component } from '@angular/core';
@@ -23,15 +23,15 @@ import { ChangeDetectionStrategy, Component } from '@angular/core';
2323
export class MyComponent {}
2424
```
2525

26-
## Common change detection scenarios
26+
## 一般的な変更検知のシナリオ
2727

28-
This section examines several common change detection scenarios to illustrate Angular's behavior.
28+
このセクションでは、Angularの動作を説明するために、いくつかの一般的な変更検知のシナリオを検証します。
2929

30-
### An event is handled by a component with default change detection
30+
### デフォルトの変更検知を持つコンポーネントによってイベントが処理される場合
3131

32-
If Angular handles an event within a component without `OnPush` strategy, the framework executes change detection on the entire component tree. Angular will skip descendant component subtrees with roots using `OnPush`, which have not received new inputs.
32+
Angularが`OnPush`戦略なしでコンポーネント内でイベントを処理する場合、フレームワークはコンポーネントツリー全体で変更検知を実行します。Angularは、新しいインプットを受け取っていない、`OnPush`を使用しているルートを持つ子孫コンポーネントのサブツリーをスキップします。
3333

34-
As an example, if we set the change detection strategy of `MainComponent` to `OnPush` and the user interacts with a component outside the subtree with root `MainComponent`, Angular will check all the pink components from the diagram below (`AppComponent`, `HeaderComponent`, `SearchComponent`, `ButtonComponent`) unless `MainComponent` receives new inputs:
34+
例として、`MainComponent`の変更検知戦略を`OnPush`に設定し、ユーザーが`MainComponent`をルートとするサブツリーの外部のコンポーネントとやり取りする場合、`MainComponent`が新しいインプットを受け取らない限り、Angularは下の図のすべてのピンク色のコンポーネント(`AppComponent``HeaderComponent``SearchComponent``ButtonComponent`)をチェックします:
3535

3636
```mermaid
3737
graph TD;
@@ -50,11 +50,11 @@ class search checkedNode
5050
class event eventNode
5151
```
5252

53-
## An event is handled by a component with OnPush
53+
## OnPushを持つコンポーネントによってイベントが処理される場合
5454

55-
If Angular handles an event within a component with OnPush strategy, the framework will execute change detection within the entire component tree. Angular will ignore component subtrees with roots using OnPush, which have not received new inputs and are outside the component which handled the event.
55+
AngularがOnPush戦略を持つコンポーネント内でイベントを処理する場合、フレームワークはコンポーネントツリー全体で変更検知を実行します。Angularは、新しいインプットを受け取っておらず、イベントを処理したコンポーネントの外部にある、OnPushを使用しているルートを持つコンポーネントのサブツリーを無視します。
5656

57-
As an example, if Angular handles an event within `MainComponent`, the framework will run change detection in the entire component tree. Angular will ignore the subtree with root `LoginComponent` because it has `OnPush` and the event happened outside of its scope.
57+
例として、Angularが`MainComponent`内でイベントを処理する場合、フレームワークはコンポーネントツリー全体で変更検知を実行します。Angularは、`LoginComponent`をルートとするサブツリーを無視します。これは、`LoginComponent``OnPush`を持ち、イベントがそのスコープ外で発生したためです。
5858

5959
```mermaid
6060
graph TD;
@@ -75,11 +75,11 @@ class details checkedNode
7575
class event eventNode
7676
```
7777

78-
## An event is handled by a descendant of a component with OnPush
78+
## OnPushを持つコンポーネントの子孫によってイベントが処理される場合
7979

80-
If Angular handles an event in a component with OnPush, the framework will execute change detection in the entire component tree, including the component’s ancestors.
80+
AngularがOnPushを持つコンポーネントでイベントを処理する場合、フレームワークはコンポーネントの祖先を含め、コンポーネントツリー全体で変更検知を実行します。
8181

82-
As an example, in the diagram below, Angular handles an event in `LoginComponent` which uses OnPush. Angular will invoke change detection in the entire component subtree including `MainComponent` (`LoginComponent`’s parent), even though `MainComponent` has `OnPush` as well. Angular checks `MainComponent` as well because `LoginComponent` is part of its view.
82+
例として、下の図では、AngularはOnPushを使用する`LoginComponent`でイベントを処理します。Angularは、`MainComponent``OnPush`があるにもかかわらず、`MainComponent``LoginComponent`の親)を含め、コンポーネントのサブツリー全体で変更検知を呼び出します。Angularは、`LoginComponent`がそのビューの一部であるため、`MainComponent`もチェックします。
8383

8484
```mermaid
8585
graph TD;
@@ -101,11 +101,11 @@ class details checkedNode
101101
class event eventNode
102102
```
103103

104-
## New inputs to component with OnPush
104+
## OnPushを持つコンポーネントへの新しいインプット
105105

106-
Angular will run change detection within a child component with `OnPush` when setting an input property as result of a template binding.
106+
Angularは、テンプレートバインディングの結果としてインプットプロパティを設定するときに、`OnPush`を持つ子コンポーネント内で変更検知を実行します。
107107

108-
For example, in the diagram below, `AppComponent` passes a new input to `MainComponent`, which has `OnPush`. Angular will run change detection in `MainComponent` but will not run change detection in `LoginComponent`, which also has `OnPush`, unless it receives new inputs as well.
108+
例えば、下の図では、`AppComponent``OnPush`を持つ`MainComponent`に新しいインプットを渡します。Angularは`MainComponent`で変更検知を実行しますが、同じく`OnPush`を持っている`LoginComponent` は新しいインプットを受け取らない限り変更検知を実行しません。
109109

110110
```mermaid
111111
graph TD;
@@ -126,7 +126,7 @@ class details checkedNode
126126
class event eventNode
127127
```
128128

129-
## Edge cases
129+
## エッジケース
130130

131-
* **Modifying input properties in TypeScript code**. When you use an API like `@ViewChild` or `@ContentChild` to get a reference to a component in TypeScript and manually modify an `@Input` property, Angular will not automatically run change detection for OnPush components. If you need Angular to run change detection, you can inject `ChangeDetectorRef` in your component and call `changeDetectorRef.markForCheck()` to tell Angular to schedule a change detection.
132-
* **Modifying object references**. In case an input receives a mutable object as value and you modify the object but preserve the reference, Angular will not invoke change detection. That’s the expected behavior because the previous and the current value of the input point to the same reference.
131+
* **TypeScriptコードでインプットプロパティを変更する**`@ViewChild``@ContentChild`のようなAPIを使用して、TypeScriptでコンポーネントへの参照を取得し、`@Input`プロパティを手動で変更すると、AngularはOnPushコンポーネントの変更検知を自動的に実行しません。Angularに変更検知を実行させる必要がある場合は、コンポーネントに`ChangeDetectorRef`を注入し、`changeDetectorRef.markForCheck()`を呼び出して、Angularに変更検知をスケジュールするように指示できます。
132+
* **オブジェクト参照の変更**。インプットが可変オブジェクトを値として受け取り、オブジェクトを変更しても参照を保持する場合、Angularは変更検知を呼び出しません。これは、インプットの以前の値と現在の値が同じ参照を指していることによる想定通りの動作です。

0 commit comments

Comments
 (0)