1e41f4b71Sopenharmony_ci# \@LocalBuilder decorator: Maintaining the Parent-Child Relationship Between Component and State Management 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciWhen use @Builder to pass data, the parent-child relationship of components is considered. After **bind(this)** is used, the parent-child relationship of components is inconsistent with that of state management. As a result, the @LocalBuilder decorator is used to fix the inconsistency. @LocalBuilder has the same features as local @Builder and provides a better determination of the parent-child relationship of components and state management. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci> **NOTE** 7e41f4b71Sopenharmony_ci> 8e41f4b71Sopenharmony_ci> This decorator is supported since API version 12. 9e41f4b71Sopenharmony_ci> 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## How to Use 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### Local Custom Builder Function 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciSyntax: 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci```ts 21e41f4b71Sopenharmony_ci@LocalBuilder MyBuilderFunction() { ... } 22e41f4b71Sopenharmony_ci``` 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciUsage: 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci```ts 28e41f4b71Sopenharmony_cithis.MyBuilderFunction() 29e41f4b71Sopenharmony_ci``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci- One or more @LocalBuilder methods can be defined in a custom component. The methods are considered as private and special member functions of the component. 32e41f4b71Sopenharmony_ci- The custom builder function can be called from the **build** method or another custom builder function in the same component only. 33e41f4b71Sopenharmony_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. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci## Constraints 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci- @LocalBuilder can be declared only within the component to which it belongs. Global declaration is not allowed. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci- @LocalBuilder cannot be used by built-in decorators and custom decorators. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- Static methods in a custom component cannot be used together with @LocalBuilder. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci## Differences Between @LocalBuilder and Local @Builder 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ciTo change the pointed object of **this**, **bind(this)** used in the local @Builder will cause inconsistent parent-child relationship between the component and the state management. However, this problem does not exist in the @LocalBuilder. For details, see [Differences between @LocalBuilder and @Builder](arkts-localBuilder.md#differences-between-localbuilder-and-builder). 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci## Parameter Passing Rules 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ciFor @LocalBuilder 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: 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_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. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci- All parameters must be immutable inside the @LocalBuilder function. 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci- The \@LocalBuilder function body follows the same [syntax rules](arkts-create-custom-components.md#build-function) as the **build()** function. 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_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. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci### By-Reference Parameter Passing 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciIn by-reference parameter passing, state variables can be passed, and the change of these state variables causes the UI re-rendering in the \@LocalBuilder decorated method. 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ciUse scenario: 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciThe @LocalBuilder method in the **Parent** component is called in the **build** function to pass the parameters by keys. When you click **Click me**, the **Text** content in the @LocalBuilder changes with the state variable. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci```ts 69e41f4b71Sopenharmony_ciclass ReferenceType { 70e41f4b71Sopenharmony_ci paramString: string = ''; 71e41f4b71Sopenharmony_ci} 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci@Entry 74e41f4b71Sopenharmony_ci@Component 75e41f4b71Sopenharmony_cistruct Parent { 76e41f4b71Sopenharmony_ci @State variableValue: string = 'Hello World'; 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci @LocalBuilder 79e41f4b71Sopenharmony_ci citeLocalBuilder(params: ReferenceType) { 80e41f4b71Sopenharmony_ci Row() { 81e41f4b71Sopenharmony_ci Text(`UseStateVarByReference: ${params.paramString} `) 82e41f4b71Sopenharmony_ci } 83e41f4b71Sopenharmony_ci }; 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci build() { 86e41f4b71Sopenharmony_ci Column() { 87e41f4b71Sopenharmony_ci this.citeLocalBuilder({ paramString: this.variableValue }); 88e41f4b71Sopenharmony_ci Button('Click me').onClick(() => { 89e41f4b71Sopenharmony_ci this.variableValue = 'Hi World'; 90e41f4b71Sopenharmony_ci }) 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci} 94e41f4b71Sopenharmony_ci``` 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciWhen parameters are passed by reference, if a custom component is called within the\@LocalBuilder method, ArkUI provides [$$](arkts-two-way-sync.md) as the paradigm for passing parameters by reference. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ciUse scenario: 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ciThe @LocalBuilder method in the **Parent** component is called in the custom component to pass the parameters by reference. When the value of a state variable in the **Parent** component changes, the **message** of the custom component **HelloComponent** in the @LocalBuilder method also changes. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci```ts 103e41f4b71Sopenharmony_ciclass ReferenceType { 104e41f4b71Sopenharmony_ci paramString: string = ''; 105e41f4b71Sopenharmony_ci} 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci@Component 108e41f4b71Sopenharmony_cistruct HelloComponent { 109e41f4b71Sopenharmony_ci @Prop message: string; 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci build() { 112e41f4b71Sopenharmony_ci Row() { 113e41f4b71Sopenharmony_ci Text(`HelloComponent===${this.message}`); 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci} 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci@Entry 119e41f4b71Sopenharmony_ci@Component 120e41f4b71Sopenharmony_cistruct Parent { 121e41f4b71Sopenharmony_ci @State variableValue: string = 'Hello World'; 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci @LocalBuilder 124e41f4b71Sopenharmony_ci citeLocalBuilder($$: ReferenceType) { 125e41f4b71Sopenharmony_ci Row() { 126e41f4b71Sopenharmony_ci Column() { 127e41f4b71Sopenharmony_ci Text(`citeLocalBuilder===${$$.paramString}`); 128e41f4b71Sopenharmony_ci HelloComponent({ message: $$.paramString }); 129e41f4b71Sopenharmony_ci } 130e41f4b71Sopenharmony_ci } 131e41f4b71Sopenharmony_ci } 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci build() { 134e41f4b71Sopenharmony_ci Column() { 135e41f4b71Sopenharmony_ci this.citeLocalBuilder({ paramString: this.variableValue }); 136e41f4b71Sopenharmony_ci Button('Click me').onClick(() => { 137e41f4b71Sopenharmony_ci this.variableValue = 'Hi World'; 138e41f4b71Sopenharmony_ci }) 139e41f4b71Sopenharmony_ci } 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci} 142e41f4b71Sopenharmony_ci``` 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci### By-Value Parameter Passing 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ciBy default, parameters in the \@LocalBuilder 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 \@LocalBuilder decorated function. Therefore, when passing state variables, you are advised to use [by-reference parameter passing](#by-reference-parameter-passing). 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ciUse scenario: 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ciThe **Parent** component passes the @State decorated **label** value to the @LocalBuilder function as a function parameter. In this case, the value obtained by the @LocalBuilder function is a common variable value. Therefore, when the @State decorated **label** value is changed, the value in the @LocalBuilder function does not change. 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci```ts 155e41f4b71Sopenharmony_ci@Entry 156e41f4b71Sopenharmony_ci@Component 157e41f4b71Sopenharmony_cistruct Parent { 158e41f4b71Sopenharmony_ci @State label: string = 'Hello'; 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci @LocalBuilder 161e41f4b71Sopenharmony_ci citeLocalBuilder(paramA1: string) { 162e41f4b71Sopenharmony_ci Row() { 163e41f4b71Sopenharmony_ci Text(`UseStateVarByValue: ${paramA1} `) 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci } 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci build() { 168e41f4b71Sopenharmony_ci Column() { 169e41f4b71Sopenharmony_ci this.citeLocalBuilder(this.label); 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci} 173e41f4b71Sopenharmony_ci``` 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci## Differences Between @LocalBuilder and @Builder 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ciWhen the **componentBuilder** function is decorated by **@Builder**, the **Child** component is displayed. When the **componentBuilder** function is decorated by @LocalBuild, **Parent** component is displayed. 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci**NOTE** 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci**componentBuilder()** of the @Builder is passed to **customBuilderParam** of the @BuilderParam child component in the form of **this.componentBuilder**. **this** points to the label of **Child**, that is, **Child** is displayed. 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci**componentBuilder()** of the @Builder is passed to **customBuilderParam** of the @BuilderParam child component in the form of **this.componentBuilder**. **this** points to the label of **Parent**, that is, **Parent** is displayed. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci```ts 186e41f4b71Sopenharmony_ci@Component 187e41f4b71Sopenharmony_cistruct Child { 188e41f4b71Sopenharmony_ci label: string = `Child`; 189e41f4b71Sopenharmony_ci @BuilderParam customBuilderParam: () => void; 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci build() { 192e41f4b71Sopenharmony_ci Column() { 193e41f4b71Sopenharmony_ci this.customBuilderParam() 194e41f4b71Sopenharmony_ci } 195e41f4b71Sopenharmony_ci } 196e41f4b71Sopenharmony_ci} 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci@Entry 199e41f4b71Sopenharmony_ci@Component 200e41f4b71Sopenharmony_cistruct Parent { 201e41f4b71Sopenharmony_ci label: string = `Parent`; 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci @Builder componentBuilder() { 204e41f4b71Sopenharmony_ci Text(`${this.label}`) 205e41f4b71Sopenharmony_ci } 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci // @LocalBuilder componentBuilder() { 208e41f4b71Sopenharmony_ci // Text(`${this.label}`) 209e41f4b71Sopenharmony_ci // } 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci build() { 212e41f4b71Sopenharmony_ci Column() { 213e41f4b71Sopenharmony_ci Child({ customBuilderParam: this.componentBuilder }) 214e41f4b71Sopenharmony_ci } 215e41f4b71Sopenharmony_ci } 216e41f4b71Sopenharmony_ci} 217e41f4b71Sopenharmony_ci``` 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci## Use Scenarios 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci### Using @LocalBuilder in @ComponentV2 Decorated Custom Components 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ciCall the @LocalBuilder in the custom component decorated by @ComponentV2 to change the variables, triggering the UI re-renders. 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci```ts 226e41f4b71Sopenharmony_ci@ObservedV2 227e41f4b71Sopenharmony_ciclass Info { 228e41f4b71Sopenharmony_ci @Trace name: string = ''; 229e41f4b71Sopenharmony_ci @Trace age: number = 0; 230e41f4b71Sopenharmony_ci} 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci@ComponentV2 233e41f4b71Sopenharmony_cistruct ChildPage { 234e41f4b71Sopenharmony_ci @Require @Param childInfo: Info; 235e41f4b71Sopenharmony_ci build() { 236e41f4b71Sopenharmony_ci Column() { 237e41f4b71Sopenharmony_ci Text(`Custom component name :${this.childInfo.name}`) 238e41f4b71Sopenharmony_ci .fontSize(20) 239e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 240e41f4b71Sopenharmony_ci Text(`Custom component age :${this.childInfo.age}`) 241e41f4b71Sopenharmony_ci .fontSize(20) 242e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 243e41f4b71Sopenharmony_ci } 244e41f4b71Sopenharmony_ci } 245e41f4b71Sopenharmony_ci} 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci@Entry 248e41f4b71Sopenharmony_ci@ComponentV2 249e41f4b71Sopenharmony_cistruct ParentPage { 250e41f4b71Sopenharmony_ci info1: Info = { name: "Tom", age: 25 }; 251e41f4b71Sopenharmony_ci @Local info2: Info = { name: "Tom", age: 25 }; 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci @LocalBuilder 254e41f4b71Sopenharmony_ci privateBuilder() { 255e41f4b71Sopenharmony_ci Column() { 256e41f4b71Sopenharmony_ci Text(`LocalBuilder@Builder name :${this.info1.name}`) 257e41f4b71Sopenharmony_ci .fontSize(20) 258e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 259e41f4b71Sopenharmony_ci Text(`LocalBuilder@Builder age :${this.info1.age}`) 260e41f4b71Sopenharmony_ci .fontSize(20) 261e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 262e41f4b71Sopenharmony_ci } 263e41f4b71Sopenharmony_ci } 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci @LocalBuilder 266e41f4b71Sopenharmony_ci privateBuilderSecond() { 267e41f4b71Sopenharmony_ci Column() { 268e41f4b71Sopenharmony_ci Text(`LocalBuilder@Builder name :${this.info2.name}`) 269e41f4b71Sopenharmony_ci .fontSize(20) 270e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 271e41f4b71Sopenharmony_ci Text(`LocalBuilder@Builder age :${this.info2.age}`) 272e41f4b71Sopenharmony_ci .fontSize(20) 273e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 274e41f4b71Sopenharmony_ci } 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci build() { 277e41f4b71Sopenharmony_ci Column() { 278e41f4b71Sopenharmony_ci Text(`info1: ${this.info1.name} ${this.info1.age}`) // Text1 279e41f4b71Sopenharmony_ci .fontSize(30) 280e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 281e41f4b71Sopenharmony_ci this.privateBuilder() // Call the local @Builder. 282e41f4b71Sopenharmony_ci Line() 283e41f4b71Sopenharmony_ci .width('100%') 284e41f4b71Sopenharmony_ci .height(10) 285e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 286e41f4b71Sopenharmony_ci Text(`info2: ${this.info2.name} ${this.info2.age}`) // Text1 287e41f4b71Sopenharmony_ci .fontSize(30) 288e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 289e41f4b71Sopenharmony_ci this.privateBuilderSecond() // Call the local @Builder. 290e41f4b71Sopenharmony_ci Line() 291e41f4b71Sopenharmony_ci .width('100%') 292e41f4b71Sopenharmony_ci .height(10) 293e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 294e41f4b71Sopenharmony_ci Text(`info1: ${this.info1.name} ${this.info1.age}`) // Text1 295e41f4b71Sopenharmony_ci .fontSize(30) 296e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 297e41f4b71Sopenharmony_ci ChildPage({childInfo: this.info1}) // Call the custom component. 298e41f4b71Sopenharmony_ci Line() 299e41f4b71Sopenharmony_ci .width('100%') 300e41f4b71Sopenharmony_ci .height(10) 301e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 302e41f4b71Sopenharmony_ci Text(`info2: ${this.info2.name} ${this.info2.age}`) // Text2 303e41f4b71Sopenharmony_ci .fontSize(30) 304e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 305e41f4b71Sopenharmony_ci ChildPage({childInfo: this.info2}) // Call the custom component. 306e41f4b71Sopenharmony_ci Line() 307e41f4b71Sopenharmony_ci .width('100%') 308e41f4b71Sopenharmony_ci .height(10) 309e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 310e41f4b71Sopenharmony_ci Button("change info1&info2") 311e41f4b71Sopenharmony_ci .onClick(() => { 312e41f4b71Sopenharmony_ci this.info1 = { name: "Cat", age: 18} // Text1 is not re-rendered because no decorator is used to listen for value changes. 313e41f4b71Sopenharmony_ci this.info2 = { name: "Cat", age: 18} // Text2 is re-rendered because a decorator is used to listen for value changes. 314e41f4b71Sopenharmony_ci }) 315e41f4b71Sopenharmony_ci } 316e41f4b71Sopenharmony_ci } 317e41f4b71Sopenharmony_ci} 318e41f4b71Sopenharmony_ci``` 319