目录

@performance/hp-arkui-replace-nested-reusable-component-by-builder

Use @Builder to replace nested custom components.

In common frame loss scenarios, modify the code according to this rule whenever possible.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@performance/hp-arkui-replace-nested-reusable-component-by-builder": "warn",
  }
}

Options

This rule is not configurable.

Example of Correct Code

// Source file, which is subject to the actual project.
import { MyDataSource } from './MyDataSource';

@Entry
@Component
struct MyComponent{
  private data: MyDataSource = new MyDataSource();

  aboutToAppear(): void {
    for (let index = 0; index < 30; index++) {
      this.data.pushData(index.toString())
    }
  }

  build() {
    Column() {
      List() {
        LazyForEach(this.data, (item: string) => {
          ListItem() {
            // Correct
            ChildComponent({ desc: item })
          }
        }, (item: string) => item)
      }
      .height('100%')
      .width('100%')
    }
    .width('100%')
  }
}

// Correct: Component reuse
@Reusable
@Component
struct ChildComponent {
  @State desc: string = '';

  aboutToReuse(params: Record<string, Object>): void {
    this.desc = params.desc as string;
  }

  build() {
    Column() {
      // Using @Builder can reduce the time it takes to create and render custom components.
      ChildComponentBuilder({ paramA: this.desc })
    }
    .width('100%')
  }
}

class Temp {
  paramA: string = '';
}

@Builder
function ChildComponentBuilder($$: Temp) {
  Column() {
    // ${} is used for passing by reference, allowing @Builder to detect data changes and update the UI.
    Text(`Child component + ${$$.paramA}`)
      .fontSize(30)
      .fontWeight(30)
  }
  .width('100%')
}

Example of Incorrect Code

// Source file, which is subject to the actual project.
import { MyDataSource } from './MyDataSource';

@Entry
@Component
struct MyComponent{
  private data: MyDataSource = new MyDataSource();

  aboutToAppear(): void {
    for (let index = 0; index < 30; index++) {
      this.data.pushData(index.toString())
    }
  }

  build() {
    Column() {
      List() {
        LazyForEach(this.data, (item: string) => {
          ListItem() {
            // Incorrect: Nesting a custom component
            ComponentA({ desc: item })
          }
        }, (item: string) => item)
      }
      .height('100%')
      .width('100%')
    }
  }
}

@Reusable
@Component
struct ComponentA {
  @State desc: string = '';

  aboutToReuse(params: ESObject): void {
    this.desc = params.desc as string;
  }

  build() {
    // Nest a custom component in a reused component.
    ComponentB({ desc: this.desc })
  }
}


@Component
struct ComponentB {
  @State desc: string = '';

  // aboutToReuse implementation is also required in nested components to update the UI.
  aboutToReuse(params: ESObject): void {
    this.desc = params.desc as string;
  }

  build() {
    Column() {
      Text('Child component' + this.desc)
        .fontSize(30)
        .fontWeight(30)
    }
    .width('100%')
  }
}

Rule Set

plugin:@performance/recommended
plugin:@performance/all

For details about how to configure Code Linter check rules, see Code Linter.