1# if/else:条件渲染
2
3
4ArkTS提供了渲染控制的能力。条件渲染可根据应用的不同状态,使用if、else和else if渲染对应状态下的UI内容。
5
6> **说明:**
7>
8> 从API version 9开始,该接口支持在ArkTS卡片中使用。
9
10## 使用规则
11
12- 支持if、else和else if语句。
13
14- if、else if后跟随的条件语句可以使用状态变量或者常规变量(状态变量:值的改变可以实时渲染UI,常规变量:值的改变不会实时渲染UI)。
15
16- 允许在容器组件内使用,通过条件渲染语句构建不同的子组件。
17
18- 条件渲染语句在涉及到组件的父子关系时是“透明”的,当父组件和子组件之间存在一个或多个if语句时,必须遵守父组件关于子组件使用的规则。
19
20- 每个分支内部的构建函数必须遵循构建函数的规则,并创建一个或多个组件。无法创建组件的空构建函数会产生语法错误。
21
22- 某些容器组件限制子组件的类型或数量,将条件渲染语句用于这些组件内时,这些限制将同样应用于条件渲染语句内创建的组件。例如,[Grid](../reference/apis-arkui/arkui-ts/ts-container-grid.md)容器组件的子组件仅支持[GridItem](../reference/apis-arkui/arkui-ts/ts-container-griditem.md)组件,在Grid内使用条件渲染语句时,条件渲染语句内仅允许使用GridItem组件。
23
24
25## 更新机制
26
27当if、else if后跟随的状态判断中使用的状态变量值变化时,条件渲染语句会进行更新,更新步骤如下:
28
291. 评估if和else if的状态判断条件,如果分支没有变化,无需执行以下步骤。如果分支有变化,则执行2、3步骤:
30
312. 删除此前构建的所有子组件。
32
333. 执行新分支的构造函数,将获取到的组件添加到if父容器中。如果缺少适用的else分支,则不构建任何内容。
34
35条件可以包括Typescript表达式。对于构造函数中的表达式,此类表达式不得更改应用程序状态。
36
37
38## 使用场景
39
40
41### 使用if进行条件渲染
42
43
44```ts
45@Entry
46@Component
47struct MyComponent {
48  @State count: number = 0;
49
50  build() {
51    Column() {
52      Text(`count=${this.count}`)
53
54      if (this.count > 0) {
55        Text(`count is positive`)
56          .fontColor(Color.Green)
57      }
58
59      Button('increase count')
60        .onClick(() => {
61          this.count++;
62        })
63
64      Button('decrease count')
65        .onClick(() => {
66          this.count--;
67        })
68    }
69  }
70}
71```
72
73if语句的每个分支都包含一个构建函数。此类构建函数必须创建一个或多个子组件。在初始渲染时,if语句会执行构建函数,并将生成的子组件添加到其父组件中。
74
75每当if或else if条件语句中使用的状态变量发生变化时,条件语句都会更新并重新评估新的条件值。如果条件值评估发生了变化,这意味着需要构建另一个条件分支。此时ArkUI框架将:
76
771. 删除所有以前渲染的(早期分支的)组件。
78
792. 执行新分支的构造函数,将生成的子组件添加到其父组件中。
80
81在以上示例中,如果count从0增加到1,那么if语句更新,条件count > 0将重新评估,评估结果将从false更改为true。因此,将执行条件为真分支的构造函数,创建一个Text组件,并将它添加到父组件Column中。如果后续count更改为0,则Text组件将从Column组件中删除。由于没有else分支,因此不会执行新的构造函数。
82
83
84### if ... else ...语句和子组件状态
85
86以下示例包含if ... else ...语句与拥有\@State装饰变量的子组件。
87
88
89```ts
90@Component
91struct CounterView {
92  @State counter: number = 0;
93  label: string = 'unknown';
94
95  build() {
96    Column({ space: 20 }) {
97      Text(`${this.label}`)
98      Button(`counter ${this.counter} +1`)
99        .onClick(() => {
100          this.counter += 1;
101        })
102    }
103    .margin(10)
104    .padding(10)
105    .border({ width: 1 })
106  }
107}
108
109@Entry
110@Component
111struct MainView {
112  @State toggle: boolean = true;
113
114  build() {
115    Column() {
116      if (this.toggle) {
117        CounterView({ label: 'CounterView #positive' })
118      } else {
119        CounterView({ label: 'CounterView #negative' })
120      }
121      Button(`toggle ${this.toggle}`)
122        .onClick(() => {
123          this.toggle = !this.toggle;
124        })
125    }
126    .width('100%')
127    .justifyContent(FlexAlign.Center)
128  }
129}
130```
131
132CounterView(label为 'CounterView \#positive')子组件在初次渲染时创建。此子组件携带名为counter的状态变量。当修改CounterView.counter状态变量时,CounterView(label为 'CounterView \#positive')子组件重新渲染并保留状态变量值。当MainView.toggle状态变量的值更改为false时,MainView父组件内的if语句将更新,随后将删除CounterView(label为 'CounterView \#positive')子组件。与此同时,将创建新的CounterView(label为 'CounterView \#negative')实例。而它自己的counter状态变量设置为初始值0。
133
134> **说明:**
135>
136> CounterView(label为 'CounterView \#positive')和CounterView(label为 'CounterView \#negative')是同一自定义组件的两个不同实例。if分支的更改,不会更新现有子组件,也不会保留状态。
137
138以下示例展示了条件更改时,若需要保留counter值所做的修改。
139
140
141```ts
142@Component
143struct CounterView {
144  @Link counter: number;
145  label: string = 'unknown';
146
147  build() {
148    Column({ space: 20 }) {
149      Text(`${this.label}`)
150        .fontSize(20)
151      Button(`counter ${this.counter} +1`)
152        .onClick(() => {
153          this.counter += 1;
154        })
155    }
156    .margin(10)
157    .padding(10)
158    .border({ width: 1 })
159  }
160}
161
162@Entry
163@Component
164struct MainView {
165  @State toggle: boolean = true;
166  @State counter: number = 0;
167
168  build() {
169    Column() {
170      if (this.toggle) {
171        CounterView({ counter: $counter, label: 'CounterView #positive' })
172      } else {
173        CounterView({ counter: $counter, label: 'CounterView #negative' })
174      }
175      Button(`toggle ${this.toggle}`)
176        .onClick(() => {
177          this.toggle = !this.toggle;
178        })
179    }
180    .width('100%')
181    .justifyContent(FlexAlign.Center)
182  }
183}
184```
185
186此处,\@State counter变量归父组件所有。因此,当CounterView组件实例被删除时,该变量不会被销毁。CounterView组件通过\@Link装饰器引用状态。状态必须从子级移动到其父级(或父级的父级),以避免在条件内容或重复内容被销毁时丢失状态。
187
188
189### 嵌套if语句
190
191条件语句的嵌套对父组件的相关规则没有影响。
192
193
194```ts
195@Entry
196@Component
197struct MyComponent {
198  @State toggle: boolean = false;
199  @State toggleColor: boolean = false;
200
201  build() {
202    Column({ space: 20 }) {
203      Text('Before')
204        .fontSize(15)
205      if (this.toggle) {
206        Text('Top True, positive 1 top')
207          .backgroundColor('#aaffaa').fontSize(20)
208        // 内部if语句
209        if (this.toggleColor) {
210          Text('Top True, Nested True, positive COLOR  Nested ')
211            .backgroundColor('#00aaaa').fontSize(15)
212        } else {
213          Text('Top True, Nested False, Negative COLOR  Nested ')
214            .backgroundColor('#aaaaff').fontSize(15)
215        }
216      } else {
217        Text('Top false, negative top level').fontSize(20)
218          .backgroundColor('#ffaaaa')
219        if (this.toggleColor) {
220          Text('positive COLOR  Nested ')
221            .backgroundColor('#00aaaa').fontSize(15)
222        } else {
223          Text('Negative COLOR  Nested ')
224            .backgroundColor('#aaaaff').fontSize(15)
225        }
226      }
227      Text('After')
228        .fontSize(15)
229      Button('Toggle Outer')
230        .onClick(() => {
231          this.toggle = !this.toggle;
232        })
233      Button('Toggle Inner')
234        .onClick(() => {
235          this.toggleColor = !this.toggleColor;
236        })
237    }
238    .width('100%')
239    .justifyContent(FlexAlign.Center)
240  }
241}
242```
243
244## 常见问题
245
246### 动效场景下if分支切换保护失效
247
248在动画当中改变IfElse分支,而这个IfElse是用来做数据保护的,继续使用该分支会导致访问数据异常,然后造成crash。
249
250反例:
251
252
253```ts
254class MyData {
255  str: string;
256  constructor(str: string) {
257    this.str = str;
258  }
259}
260@Entry
261@Component
262struct Index {
263  @State data1: MyData|undefined = new MyData("branch 0");
264  @State data2: MyData|undefined = new MyData("branch 1");
265
266  build() {
267    Column() {
268      if (this.data1) {
269        // 如果在动画中增加/删除,会给Text增加默认转场
270        // 对于删除时,增加默认透明度转场后,会延长组件的生命周期,Text组件没有真正删除,而是等转场动画做完后才删除
271        Text(this.data1.str)
272          .id("1")
273      } else if (this.data2) {
274        // 如果在动画中增加/删除,会给Text增加默认转场
275        Text(this.data2.str)
276          .id("2")
277      }
278
279      Button("play with animation")
280        .onClick(() => {
281          animateTo({}, ()=>{
282            // 在animateTo中修改if条件,在动画当中,会给if下的第一层组件默认转场
283            if (this.data1) {
284              this.data1 = undefined;
285              this.data2 = new MyData("branch 1");
286            } else {
287              this.data1 = new MyData("branch 0");
288              this.data2 = undefined;
289            }
290          })
291        })
292
293      Button("play directlp")
294        .onClick(() => {
295          // 直接改if条件,不在动画当中,可以正常切换,也不会加默认转场
296          if (this.data1) {
297            this.data1 = undefined;
298            this.data2 = new MyData("branch 1");
299          } else {
300            this.data1 = new MyData("branch 0");
301            this.data2 = undefined;
302          }
303        })
304    }.width("100%")
305    .padding(10)
306  }
307}
308```
309
310正例:
311
312方式1:给数据继续加判空的保护,即在使用data时再加一层判空,即"Text(this.data1?.str)"。
313
314
315```ts
316class MyData {
317  str: string;
318  constructor(str: string) {
319    this.str = str;
320  }
321}
322@Entry
323@Component
324struct Index {
325  @State data1: MyData|undefined = new MyData("branch 0");
326  @State data2: MyData|undefined = new MyData("branch 1");
327
328  build() {
329    Column() {
330      if (this.data1) {
331        // 如果在动画中增加/删除,会给Text增加默认转场
332        // 对于删除时,增加默认透明度转场后,会延长组件的生命周期,Text组件没有真正删除,而是等转场动画做完后才删除
333        // 在使用数据时再加一层判空保护,如果data1存在才去使用data1当中的str
334        Text(this.data1?.str)
335          .id("1")
336      } else if (this.data2) {
337        // 如果在动画中增加/删除,会给Text增加默认转场
338        // 在使用数据时再加一层判空保护
339        Text(this.data2?.str)
340          .id("2")
341      }
342
343      Button("play with animation")
344        .onClick(() => {
345          animateTo({}, ()=>{
346            // 在animateTo中修改if条件,在动画当中,会给if下的第一层组件默认转场
347            if (this.data1) {
348              this.data1 = undefined;
349              this.data2 = new MyData("branch 1");
350            } else {
351              this.data1 = new MyData("branch 0");
352              this.data2 = undefined;
353            }
354          })
355        })
356    }.width("100%")
357    .padding(10)
358  }
359}
360```
361
362方式2:给IfElse下直接要被删除的组件显示的添加transition(TransitionEffect.IDENTITY)属性,避免系统添加默认转场。
363
364
365```ts
366class MyData {
367  str: string;
368  constructor(str: string) {
369    this.str = str;
370  }
371}
372@Entry
373@Component
374struct Index {
375  @State data1: MyData|undefined = new MyData("branch 0");
376  @State data2: MyData|undefined = new MyData("branch 1");
377
378  build() {
379    Column() {
380      if (this.data1) {
381        // 在IfElse的根组件显示指定空的转场效果,避免默认转场动画
382        Text(this.data1.str)
383          .transition(TransitionEffect.IDENTITY)
384          .id("1")
385      } else if (this.data2) {
386        // 在IfElse的根组件显示指定空的转场效果,避免默认转场动画
387        Text(this.data2.str)
388          .transition(TransitionEffect.IDENTITY)
389          .id("2")
390      }
391
392      Button("play with animation")
393        .onClick(() => {
394          animateTo({}, ()=>{
395            // 在animateTo中修改if条件,在动画当中,会给if下的第一层组件默认转场
396            // 但由于已经显示指定转场了就不会再添加默认转场
397            if (this.data1) {
398              this.data1 = undefined;
399              this.data2 = new MyData("branch 1");
400            } else {
401              this.data1 = new MyData("branch 0");
402              this.data2 = undefined;
403            }
404          })
405        })
406    }.width("100%")
407    .padding(10)
408  }
409}
410```
411
412
413