File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 引数を持つmodifierを提供する
2
+
3
+ 引数内で状態の条件分岐を記述できるよう、引数を持つmodifierを提供します。
4
+
5
+ ## Overview
6
+
7
+ SwiftUIでは、条件に応じてViewの見た目や振る舞いを変更することがよくあります。引数のないmodifierを使用する場合、利用側は` if ` 文による条件分岐を使用するしかありませんが、これはView identityが変更され、パフォーマンスの低下を招きます。
8
+
9
+ 引数を持つmodifierを提供することで、利用側はmodifierの引数の中で条件分岐を記述でき、View identityを保持したままmodifierを条件によって変更できます。
10
+
11
+ ### 引数のないmodifierの問題点
12
+
13
+ 引数のないmodifierを条件分岐によって切り替えるには、` if ` 文を使用する必要があります:
14
+
15
+ ``` swift
16
+
17
+ struct ContentView : View {
18
+ @State var condition = false
19
+
20
+ var body: some View {
21
+ xxx
22
+ }
23
+ }
24
+ ```
25
+
26
+ この実装では、` condition ` の値が変わるたびに、` Text ` がView identityの異なるViewに再構築されます。これにより、状態変更のたびに完全な再構築が必要となり、パフォーマンスが低下します。
27
+
28
+ ### 条件分岐をmodifierの引数に移動する
29
+
30
+ 引数を持つmodifierを提供すると、条件分岐を引数に移動することで同一のView identityで表現できます:
31
+
32
+ ``` swift
33
+ xxx
34
+ ```
You can’t perform that action at this time.
0 commit comments