1e41f4b71Sopenharmony_ci# \@Builder Decorator: Custom Builder Function 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciArkUI provides the \@Builder decorator that is a lightweight UI element reuse mechanism. This custom component has a fixed internal UI structure and passes the data only to the user. You can abstract reused UI elements into a method and call the method in the **build** method. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciFor simplicity, here we refer to an \@Builder decorated function also as a custom builder function. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci> **NOTE** 9e41f4b71Sopenharmony_ci> 10e41f4b71Sopenharmony_ci> This decorator can be used in ArkTS widgets since API version 9. 11e41f4b71Sopenharmony_ci> 12e41f4b71Sopenharmony_ci> This decorator can be used in atomic services since API version 11. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci## Constraints 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci- The \@Builder triggers dynamic UI rendering for only when parameters are passed in by reference. Only one parameter can be passed. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci- If the \@Builder passes in two or more parameters, dynamic UI rendering is not triggered. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci- If the \@Builder passes in parameters by value and by reference, dynamic UI rendering is not triggered. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci- \@Builder parameters must be passed in one by one in the form of object literals to trigger dynamic UI rendering. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci## Rules of Use 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci### Private Custom Builder Function 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ciSyntax: 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci```ts 31e41f4b71Sopenharmony_ci@Builder MyBuilderFunction() {} 32e41f4b71Sopenharmony_ci``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciUsage: 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci```ts 37e41f4b71Sopenharmony_cithis.MyBuilderFunction() 38e41f4b71Sopenharmony_ci``` 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci- You can define one or more @Builder decorated methods in a custom component. Such a method is considered as a private, special type of member function of the component. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci- The custom builder function can be called from the **build** method or another custom builder function in the same component only. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci- Inside the custom builder function body, **this** refers to the owning component. Component state variables are accessible from within the custom builder function implementation. Using **this** to access the custom components' state variables is recommended over parameter passing. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci### Global Custom Builder Function 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ciSyntax: 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci```ts 52e41f4b71Sopenharmony_ci@Builder function MyGlobalBuilderFunction() { ... } 53e41f4b71Sopenharmony_ci``` 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciUsage: 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci```ts 58e41f4b71Sopenharmony_ciMyGlobalBuilderFunction() 59e41f4b71Sopenharmony_ci``` 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci- Use of a global custom builder function is recommended if no own state is involved. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci## Parameter Passing Rules 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciFor custom builder functions, parameters can be passed [by value](#by-value-parameter-passing) and [by reference](#by-reference-parameter-passing). Both of them must comply with the following rules: 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci- The parameter type must be the same as the declared parameter type. The **undefined** or **null** constants as well as expressions evaluating to these values are not allowed. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci- All parameters must be immutable inside the custom builder function. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci- The custom builder function body follows the same [syntax rules](arkts-create-custom-components.md#build-function) as the **build()** function. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci- Parameters are passed by value in all cases except when only one parameter is passed in and the parameter needs to be directly passed to the object literal. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci### By-Reference Parameter Passing 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ciIn by-reference parameter passing, state variables can be passed, and the change of these state variables causes the UI re-rendering in the \@Builder decorated method. 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci```ts 82e41f4b71Sopenharmony_ciclass Tmp { 83e41f4b71Sopenharmony_ci paramA1: string = '' 84e41f4b71Sopenharmony_ci} 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci@Builder function overBuilder(params: Tmp) { 87e41f4b71Sopenharmony_ci Row() { 88e41f4b71Sopenharmony_ci Text(`UseStateVarByReference: ${params.paramA1} `) 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci} 91e41f4b71Sopenharmony_ci@Entry 92e41f4b71Sopenharmony_ci@Component 93e41f4b71Sopenharmony_cistruct Parent { 94e41f4b71Sopenharmony_ci @State label: string = 'Hello'; 95e41f4b71Sopenharmony_ci build() { 96e41f4b71Sopenharmony_ci Column() { 97e41f4b71Sopenharmony_ci // Pass the this.label reference to the overBuilder component when the overBuilder component is called in the Parent component. 98e41f4b71Sopenharmony_ci overBuilder({ paramA1: this.label }) 99e41f4b71Sopenharmony_ci Button('Click me').onClick(() => { 100e41f4b71Sopenharmony_ci // After Click me is clicked, the UI text changes from Hello to ArkUI. 101e41f4b71Sopenharmony_ci this.label = 'ArkUI'; 102e41f4b71Sopenharmony_ci }) 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci } 105e41f4b71Sopenharmony_ci} 106e41f4b71Sopenharmony_ci``` 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciWhen parameters are passed by reference, if a custom component is called within the \@Builder method, ArkUI provides [$$](arkts-two-way-sync.md) as the paradigm for passing parameters by reference. 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci```ts 111e41f4b71Sopenharmony_ciclass Tmp { 112e41f4b71Sopenharmony_ci paramA1: string = '' 113e41f4b71Sopenharmony_ci} 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci@Builder function overBuilder($$: Tmp) { 116e41f4b71Sopenharmony_ci Row() { 117e41f4b71Sopenharmony_ci Column() { 118e41f4b71Sopenharmony_ci Text(`overBuilder===${$$.paramA1}`) 119e41f4b71Sopenharmony_ci HelloComponent({message: $$.paramA1}) 120e41f4b71Sopenharmony_ci } 121e41f4b71Sopenharmony_ci } 122e41f4b71Sopenharmony_ci} 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci@Component 125e41f4b71Sopenharmony_cistruct HelloComponent { 126e41f4b71Sopenharmony_ci @Prop message: string; 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci build() { 129e41f4b71Sopenharmony_ci Row() { 130e41f4b71Sopenharmony_ci Text(`HelloComponent===${this.message}`) 131e41f4b71Sopenharmony_ci } 132e41f4b71Sopenharmony_ci } 133e41f4b71Sopenharmony_ci} 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci@Entry 136e41f4b71Sopenharmony_ci@Component 137e41f4b71Sopenharmony_cistruct Parent { 138e41f4b71Sopenharmony_ci @State label: string = 'Hello'; 139e41f4b71Sopenharmony_ci build() { 140e41f4b71Sopenharmony_ci Column() { 141e41f4b71Sopenharmony_ci // Pass the this.label reference to the overBuilder component when the overBuilder component is called in the Parent component. 142e41f4b71Sopenharmony_ci overBuilder({paramA1: this.label}) 143e41f4b71Sopenharmony_ci Button('Click me').onClick(() => { 144e41f4b71Sopenharmony_ci // After Click me is clicked, the UI text changes from Hello to ArkUI. 145e41f4b71Sopenharmony_ci this.label = 'ArkUI'; 146e41f4b71Sopenharmony_ci }) 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci} 150e41f4b71Sopenharmony_ci``` 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci### By-Value Parameter Passing 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ciBy default, parameters in the \@Builder decorated functions are passed by value. In this case, when the passed parameter is a state variable, the change of the state variable does not cause UI re-rendering in the \@Builder decorated function. Therefore, when passing state variables, you are advised to use [by-reference parameter passing](#by-reference-parameter-passing). 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci```ts 158e41f4b71Sopenharmony_ci@Builder function overBuilder(paramA1: string) { 159e41f4b71Sopenharmony_ci Row() { 160e41f4b71Sopenharmony_ci Text(`UseStateVarByValue: ${paramA1} `) 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci} 163e41f4b71Sopenharmony_ci@Entry 164e41f4b71Sopenharmony_ci@Component 165e41f4b71Sopenharmony_cistruct Parent { 166e41f4b71Sopenharmony_ci @State label: string = 'Hello'; 167e41f4b71Sopenharmony_ci build() { 168e41f4b71Sopenharmony_ci Column() { 169e41f4b71Sopenharmony_ci overBuilder(this.label) 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci} 173e41f4b71Sopenharmony_ci``` 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci## Use Scenarios 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci### Using Custom Builder Function in Custom Component 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ciCreate a private \@Builder method, call this method by using **this.builder()** in **Column**, and change the content of **builder_value** through the **aboutToAppear** lifecycle function and Button click event to dynamically render the UI. 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci```ts 182e41f4b71Sopenharmony_ci@Entry 183e41f4b71Sopenharmony_ci@Component 184e41f4b71Sopenharmony_cistruct PrivateBuilder { 185e41f4b71Sopenharmony_ci @State builder_value: string = 'Hello'; 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci @Builder builder() { 188e41f4b71Sopenharmony_ci Column(){ 189e41f4b71Sopenharmony_ci Text(this.builder_value) 190e41f4b71Sopenharmony_ci .fontSize(30) 191e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 192e41f4b71Sopenharmony_ci } 193e41f4b71Sopenharmony_ci } 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci aboutToAppear(): void { 196e41f4b71Sopenharmony_ci setTimeout(() => { 197e41f4b71Sopenharmony_ci this.builder_value = 'Hello World'; 198e41f4b71Sopenharmony_ci },3000) 199e41f4b71Sopenharmony_ci } 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci build() { 202e41f4b71Sopenharmony_ci Row() { 203e41f4b71Sopenharmony_ci Column() { 204e41f4b71Sopenharmony_ci Text(this.builder_value) 205e41f4b71Sopenharmony_ci .fontSize(30) 206e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 207e41f4b71Sopenharmony_ci this.builder() 208e41f4b71Sopenharmony_ci Button('Click to change builder_value') 209e41f4b71Sopenharmony_ci .onClick(() => { 210e41f4b71Sopenharmony_ci this.builder_value = 'builder_value clicked' 211e41f4b71Sopenharmony_ci }) 212e41f4b71Sopenharmony_ci } 213e41f4b71Sopenharmony_ci } 214e41f4b71Sopenharmony_ci } 215e41f4b71Sopenharmony_ci} 216e41f4b71Sopenharmony_ci``` 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci### Using Global Custom Builder Function 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ciCreate a global \@Builder method and call this method by using **overBuilder()** in **Column**. Pass the simple type or complex type parameters in the form of object literals, value changes will trigger UI re-rendering. 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci```ts 223e41f4b71Sopenharmony_ciclass ChildTmp { 224e41f4b71Sopenharmony_ci val: number = 1; 225e41f4b71Sopenharmony_ci} 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ciclass Tmp { 228e41f4b71Sopenharmony_ci str_value: string = 'Hello'; 229e41f4b71Sopenharmony_ci num_value: number = 0; 230e41f4b71Sopenharmony_ci tmp_value: ChildTmp = new ChildTmp(); 231e41f4b71Sopenharmony_ci arrayTmp_value: Array<ChildTmp> = []; 232e41f4b71Sopenharmony_ci} 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci@Builder function overBuilder(param: Tmp) { 235e41f4b71Sopenharmony_ci Column() { 236e41f4b71Sopenharmony_ci Text(`str_value: ${param.str_value}`) 237e41f4b71Sopenharmony_ci Text(`num_value: ${param.num_value}`) 238e41f4b71Sopenharmony_ci Text(`tmp_value: ${param.tmp_value.val}`) 239e41f4b71Sopenharmony_ci ForEach(param.arrayTmp_value, (item: ChildTmp) => { 240e41f4b71Sopenharmony_ci Text(`arrayTmp_value: ${item.val}`) 241e41f4b71Sopenharmony_ci }, (item: ChildTmp) => JSON.stringify(item)) 242e41f4b71Sopenharmony_ci } 243e41f4b71Sopenharmony_ci} 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci@Entry 246e41f4b71Sopenharmony_ci@Component 247e41f4b71Sopenharmony_cistruct Parent { 248e41f4b71Sopenharmony_ci @State objParam: Tmp = new Tmp(); 249e41f4b71Sopenharmony_ci build() { 250e41f4b71Sopenharmony_ci Column() { 251e41f4b71Sopenharmony_ci Text('Render the UI by calling the @Builder') 252e41f4b71Sopenharmony_ci .fontSize(20) 253e41f4b71Sopenharmony_ci overBuilder({str_value: this.objParam.str_value, num_value: this.objParam.num_value, tmp_value: this.objParam.tmp_value, arrayTmp_value: this.objParam.arrayTmp_value}) 254e41f4b71Sopenharmony_ci Line() 255e41f4b71Sopenharmony_ci .width('100%') 256e41f4b71Sopenharmony_ci .height(10) 257e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 258e41f4b71Sopenharmony_ci Button('Click to change parameter').onClick(() => { 259e41f4b71Sopenharmony_ci this.objParam.str_value = 'Hello World'; 260e41f4b71Sopenharmony_ci this.objParam.num_value = 1; 261e41f4b71Sopenharmony_ci this.objParam.tmp_value.val = 8; 262e41f4b71Sopenharmony_ci const child_value: ChildTmp = { 263e41f4b71Sopenharmony_ci val: 2 264e41f4b71Sopenharmony_ci } 265e41f4b71Sopenharmony_ci this.objParam.arrayTmp_value.push(child_value) 266e41f4b71Sopenharmony_ci }) 267e41f4b71Sopenharmony_ci } 268e41f4b71Sopenharmony_ci } 269e41f4b71Sopenharmony_ci} 270e41f4b71Sopenharmony_ci``` 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci### Changing the Variables Decorated by the Decorator Triggers UI Re-rendering 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ciIn this case, the decorator feature is used. The change of the listening value triggers UI re-rendering and parameters are not passed through the \@Builder. 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci```ts 277e41f4b71Sopenharmony_ciclass Tmp { 278e41f4b71Sopenharmony_ci str_value: string = 'Hello'; 279e41f4b71Sopenharmony_ci} 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci@Entry 282e41f4b71Sopenharmony_ci@Component 283e41f4b71Sopenharmony_cistruct Parent { 284e41f4b71Sopenharmony_ci @State objParam: Tmp = new Tmp(); 285e41f4b71Sopenharmony_ci @State label: string = 'World'; 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci @Builder privateBuilder() { 288e41f4b71Sopenharmony_ci Column() { 289e41f4b71Sopenharmony_ci Text(`wrapBuilder str_value: ${this.objParam.str_value}`) 290e41f4b71Sopenharmony_ci Text(`wrapBuilder num: ${this.label}`) 291e41f4b71Sopenharmony_ci } 292e41f4b71Sopenharmony_ci } 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci build() { 295e41f4b71Sopenharmony_ci Column() { 296e41f4b71Sopenharmony_ci Text('Render the UI by calling the @Builder') 297e41f4b71Sopenharmony_ci .fontSize(20) 298e41f4b71Sopenharmony_ci this.privateBuilder() 299e41f4b71Sopenharmony_ci Line() 300e41f4b71Sopenharmony_ci .width('100%') 301e41f4b71Sopenharmony_ci .height(10) 302e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 303e41f4b71Sopenharmony_ci Button('Click to change parameter').onClick(() => { 304e41f4b71Sopenharmony_ci this.objParam.str_value = 'str_value Hello World'; 305e41f4b71Sopenharmony_ci this.label = 'label Hello World' 306e41f4b71Sopenharmony_ci }) 307e41f4b71Sopenharmony_ci } 308e41f4b71Sopenharmony_ci } 309e41f4b71Sopenharmony_ci} 310e41f4b71Sopenharmony_ci``` 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci### Using the Global and Local @Builder to Pass in Parameters of the customBuilder Type 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci```ts 315e41f4b71Sopenharmony_ci@Builder 316e41f4b71Sopenharmony_cifunction overBuilder() { 317e41f4b71Sopenharmony_ci Row() { 318e41f4b71Sopenharmony_ci Text('Global Builder') 319e41f4b71Sopenharmony_ci .fontSize(30) 320e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 321e41f4b71Sopenharmony_ci } 322e41f4b71Sopenharmony_ci} 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci@Entry 325e41f4b71Sopenharmony_ci@Component 326e41f4b71Sopenharmony_cistruct customBuilderDemo { 327e41f4b71Sopenharmony_ci @State arr: number[] = [0, 1, 2, 3, 4]; 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci @Builder privateBuilder() { 330e41f4b71Sopenharmony_ci Row() { 331e41f4b71Sopenharmony_ci Text('Local Builder') 332e41f4b71Sopenharmony_ci .fontSize(30) 333e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 334e41f4b71Sopenharmony_ci } 335e41f4b71Sopenharmony_ci } 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci build() { 338e41f4b71Sopenharmony_ci Column() { 339e41f4b71Sopenharmony_ci List({ space: 10 }) { 340e41f4b71Sopenharmony_ci ForEach(this.arr, (item: number) => { 341e41f4b71Sopenharmony_ci ListItem(){ 342e41f4b71Sopenharmony_ci Text(`${item}`) 343e41f4b71Sopenharmony_ci .width('100%') 344e41f4b71Sopenharmony_ci .height(100) 345e41f4b71Sopenharmony_ci .fontSize(16) 346e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 347e41f4b71Sopenharmony_ci .borderRadius(10) 348e41f4b71Sopenharmony_ci .backgroundColor(0xFFFFFF) 349e41f4b71Sopenharmony_ci } 350e41f4b71Sopenharmony_ci .swipeAction({ 351e41f4b71Sopenharmony_ci start: { 352e41f4b71Sopenharmony_ci builder: overBuilder() 353e41f4b71Sopenharmony_ci }, 354e41f4b71Sopenharmony_ci end: { 355e41f4b71Sopenharmony_ci builder: () => { this.privateBuilder() } 356e41f4b71Sopenharmony_ci } 357e41f4b71Sopenharmony_ci }) 358e41f4b71Sopenharmony_ci }, (item: string) => JSON.stringify(item)) 359e41f4b71Sopenharmony_ci } 360e41f4b71Sopenharmony_ci } 361e41f4b71Sopenharmony_ci } 362e41f4b71Sopenharmony_ci} 363e41f4b71Sopenharmony_ci``` 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci### Nesting of Multi-layer \@Builder Method 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ciCall the custom components or other methods within \@Builder method. ArkUI provides [$$](arkts-two-way-sync.md) as a paradigm for passing parameters by reference. 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci```ts 370e41f4b71Sopenharmony_ciclass Tmp { 371e41f4b71Sopenharmony_ci paramA1: string = ''; 372e41f4b71Sopenharmony_ci} 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci@Builder function parentBuilder($$: Tmp) { 375e41f4b71Sopenharmony_ci Row() { 376e41f4b71Sopenharmony_ci Column() { 377e41f4b71Sopenharmony_ci Text(`parentBuilder===${$$.paramA1}`) 378e41f4b71Sopenharmony_ci .fontSize(30) 379e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 380e41f4b71Sopenharmony_ci HelloComponent({message: $$.paramA1}) 381e41f4b71Sopenharmony_ci childBuilder({paramA1: $$.paramA1}) 382e41f4b71Sopenharmony_ci } 383e41f4b71Sopenharmony_ci } 384e41f4b71Sopenharmony_ci} 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ci@Component 387e41f4b71Sopenharmony_cistruct HelloComponent { 388e41f4b71Sopenharmony_ci @Prop message: string = ''; 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci build() { 391e41f4b71Sopenharmony_ci Row() { 392e41f4b71Sopenharmony_ci Text(`HelloComponent===${this.message}`) 393e41f4b71Sopenharmony_ci .fontSize(30) 394e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 395e41f4b71Sopenharmony_ci } 396e41f4b71Sopenharmony_ci } 397e41f4b71Sopenharmony_ci} 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci@Builder 400e41f4b71Sopenharmony_cifunction childBuilder($$: Tmp) { 401e41f4b71Sopenharmony_ci Row() { 402e41f4b71Sopenharmony_ci Column() { 403e41f4b71Sopenharmony_ci Text(`childBuilder===${$$.paramA1}`) 404e41f4b71Sopenharmony_ci .fontSize(30) 405e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 406e41f4b71Sopenharmony_ci HelloChildComponent({message: $$.paramA1}) 407e41f4b71Sopenharmony_ci grandsonBuilder({paramA1: $$.paramA1}) 408e41f4b71Sopenharmony_ci } 409e41f4b71Sopenharmony_ci } 410e41f4b71Sopenharmony_ci} 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci@Component 413e41f4b71Sopenharmony_cistruct HelloChildComponent { 414e41f4b71Sopenharmony_ci @State message: string = ''; 415e41f4b71Sopenharmony_ci build() { 416e41f4b71Sopenharmony_ci Row() { 417e41f4b71Sopenharmony_ci Text(`HelloChildComponent===${this.message}`) 418e41f4b71Sopenharmony_ci .fontSize(30) 419e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 420e41f4b71Sopenharmony_ci } 421e41f4b71Sopenharmony_ci } 422e41f4b71Sopenharmony_ci} 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ci@Builder function grandsonBuilder($$: Tmp) { 425e41f4b71Sopenharmony_ci Row() { 426e41f4b71Sopenharmony_ci Column() { 427e41f4b71Sopenharmony_ci Text(`grandsonBuilder===${$$.paramA1}`) 428e41f4b71Sopenharmony_ci .fontSize(30) 429e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 430e41f4b71Sopenharmony_ci HelloGrandsonComponent({message: $$.paramA1}) 431e41f4b71Sopenharmony_ci } 432e41f4b71Sopenharmony_ci } 433e41f4b71Sopenharmony_ci} 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci@Component 436e41f4b71Sopenharmony_cistruct HelloGrandsonComponent { 437e41f4b71Sopenharmony_ci @Prop message: string; 438e41f4b71Sopenharmony_ci build() { 439e41f4b71Sopenharmony_ci Row() { 440e41f4b71Sopenharmony_ci Text(`HelloGrandsonComponent===${this.message}`) 441e41f4b71Sopenharmony_ci .fontSize(30) 442e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 443e41f4b71Sopenharmony_ci } 444e41f4b71Sopenharmony_ci } 445e41f4b71Sopenharmony_ci} 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci@Entry 448e41f4b71Sopenharmony_ci@Component 449e41f4b71Sopenharmony_cistruct Parent { 450e41f4b71Sopenharmony_ci @State label: string = 'Hello'; 451e41f4b71Sopenharmony_ci build() { 452e41f4b71Sopenharmony_ci Column() { 453e41f4b71Sopenharmony_ci parentBuilder({paramA1: this.label}) 454e41f4b71Sopenharmony_ci Button('Click me').onClick(() => { 455e41f4b71Sopenharmony_ci this.label = 'ArkUI'; 456e41f4b71Sopenharmony_ci }) 457e41f4b71Sopenharmony_ci } 458e41f4b71Sopenharmony_ci } 459e41f4b71Sopenharmony_ci} 460e41f4b71Sopenharmony_ci``` 461e41f4b71Sopenharmony_ci 462e41f4b71Sopenharmony_ci## FAQs 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci### Two or More Parameters Are Used in the \@Builder 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_ciWhen two or more parameters are used, the value change does not trigger the UI re-rendering even if the parameters are passed in the form of object literals. 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ci[Incorrect Example] 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci```ts 471e41f4b71Sopenharmony_ciclass GlobalTmp { 472e41f4b71Sopenharmony_ci str_value: string = 'Hello'; 473e41f4b71Sopenharmony_ci} 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci@Builder function overBuilder(param: GlobalTmp, num: number) { 476e41f4b71Sopenharmony_ci Column() { 477e41f4b71Sopenharmony_ci Text(`str_value: ${param.str_value}`) 478e41f4b71Sopenharmony_ci Text(`num: ${num}`) 479e41f4b71Sopenharmony_ci } 480e41f4b71Sopenharmony_ci} 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci@Entry 483e41f4b71Sopenharmony_ci@Component 484e41f4b71Sopenharmony_cistruct Parent { 485e41f4b71Sopenharmony_ci @State objParam: GlobalTmp = new GlobalTmp(); 486e41f4b71Sopenharmony_ci @State num: number = 0; 487e41f4b71Sopenharmony_ci build() { 488e41f4b71Sopenharmony_ci Column() { 489e41f4b71Sopenharmony_ci Text('Render the UI by calling the @Builder') 490e41f4b71Sopenharmony_ci .fontSize(20) 491e41f4b71Sopenharmony_ci overBuilder({str_value: this.objParam.str_value}, this.num) // Two parameters are used. 492e41f4b71Sopenharmony_ci Line() 493e41f4b71Sopenharmony_ci .width('100%') 494e41f4b71Sopenharmony_ci .height(10) 495e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 496e41f4b71Sopenharmony_ci Button('Click to change parameter').onClick(() => { 497e41f4b71Sopenharmony_ci this.objParam.str_value = 'Hello World'; 498e41f4b71Sopenharmony_ci this.num = 1; 499e41f4b71Sopenharmony_ci }) 500e41f4b71Sopenharmony_ci } 501e41f4b71Sopenharmony_ci } 502e41f4b71Sopenharmony_ci} 503e41f4b71Sopenharmony_ci``` 504e41f4b71Sopenharmony_ci 505e41f4b71Sopenharmony_ci[Incorrect Example] 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci```ts 508e41f4b71Sopenharmony_ciclass GlobalTmp { 509e41f4b71Sopenharmony_ci str_value: string = 'Hello'; 510e41f4b71Sopenharmony_ci} 511e41f4b71Sopenharmony_ciclass SecondTmp { 512e41f4b71Sopenharmony_ci num_value: number = 0; 513e41f4b71Sopenharmony_ci} 514e41f4b71Sopenharmony_ci@Builder function overBuilder(param: GlobalTmp, num: SecondTmp) { 515e41f4b71Sopenharmony_ci Column() { 516e41f4b71Sopenharmony_ci Text(`str_value: ${param.str_value}`) 517e41f4b71Sopenharmony_ci Text(`num: ${num.num_value}`) 518e41f4b71Sopenharmony_ci } 519e41f4b71Sopenharmony_ci} 520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ci@Entry 522e41f4b71Sopenharmony_ci@Component 523e41f4b71Sopenharmony_cistruct Parent { 524e41f4b71Sopenharmony_ci @State strParam: GlobalTmp = new GlobalTmp(); 525e41f4b71Sopenharmony_ci @State numParam: SecondTmp = new SecondTmp(); 526e41f4b71Sopenharmony_ci build() { 527e41f4b71Sopenharmony_ci Column() { 528e41f4b71Sopenharmony_ci Text('Render the UI by calling the @Builder') 529e41f4b71Sopenharmony_ci .fontSize(20) 530e41f4b71Sopenharmony_ci overBuilder({str_value: this.strParam.str_value}, {num_value: this.numParam.num_value}) // Two parameters are used. 531e41f4b71Sopenharmony_ci Line() 532e41f4b71Sopenharmony_ci .width('100%') 533e41f4b71Sopenharmony_ci .height(10) 534e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 535e41f4b71Sopenharmony_ci Button('Click to change parameter').onClick(() => { 536e41f4b71Sopenharmony_ci this.strParam.str_value = 'Hello World'; 537e41f4b71Sopenharmony_ci this.numParam.num_value = 1; 538e41f4b71Sopenharmony_ci }) 539e41f4b71Sopenharmony_ci } 540e41f4b71Sopenharmony_ci } 541e41f4b71Sopenharmony_ci} 542e41f4b71Sopenharmony_ci``` 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ciOnly one parameter can be used in the \@Builder. When one parameter is passed in the form of object literals, the value change triggers the UI re-rendering. 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci[Correct Example] 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci```ts 549e41f4b71Sopenharmony_ciclass GlobalTmp { 550e41f4b71Sopenharmony_ci str_value: string = 'Hello'; 551e41f4b71Sopenharmony_ci num_value: number = 0; 552e41f4b71Sopenharmony_ci} 553e41f4b71Sopenharmony_ci@Builder function overBuilder(param: GlobalTmp) { 554e41f4b71Sopenharmony_ci Column() { 555e41f4b71Sopenharmony_ci Text(`str_value: ${param.str_value}`) 556e41f4b71Sopenharmony_ci Text(`num: ${param.num_value}`) 557e41f4b71Sopenharmony_ci } 558e41f4b71Sopenharmony_ci} 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci@Entry 561e41f4b71Sopenharmony_ci@Component 562e41f4b71Sopenharmony_cistruct Parent { 563e41f4b71Sopenharmony_ci @State objParam: GlobalTmp = new GlobalTmp(); 564e41f4b71Sopenharmony_ci build() { 565e41f4b71Sopenharmony_ci Column() { 566e41f4b71Sopenharmony_ci Text('Render the UI by calling the @Builder') 567e41f4b71Sopenharmony_ci .fontSize(20) 568e41f4b71Sopenharmony_ci overBuilder({str_value: this.objParam.str_value, num_value: this.objParam.num_value}) 569e41f4b71Sopenharmony_ci Line() 570e41f4b71Sopenharmony_ci .width('100%') 571e41f4b71Sopenharmony_ci .height(10) 572e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 573e41f4b71Sopenharmony_ci Button('Click to change parameter').onClick(() => { 574e41f4b71Sopenharmony_ci this.objParam.str_value = 'Hello World'; 575e41f4b71Sopenharmony_ci this.objParam.num_value = 1; 576e41f4b71Sopenharmony_ci }) 577e41f4b71Sopenharmony_ci } 578e41f4b71Sopenharmony_ci } 579e41f4b71Sopenharmony_ci} 580e41f4b71Sopenharmony_ci``` 581