跳到主要内容

@performance/hp-arkui-no-state-var-access-in-loop

避免在for、while等循环逻辑中频繁读取状态变量。

通用丢帧场景下,建议优先修改。

规则配置

// code-linter.json5
{
"rules": {
"@performance/hp-arkui-no-state-var-access-in-loop": "warn",
}
}

选项

该规则无需配置额外选项。

正例

import hilog from '@ohos.hilog'

@Entry
@Component
struct MyComponent{
@State message: string = '';
build() {
Column() {
Button('点击打印日志')
.onClick(() => {
this.message = 'click';
let logMessage: string = this.message;
for (let i = 0; i < 10; i++) {
hilog.info(0x0000, 'TAG', '%{public}s', logMessage);
}
})
.width('90%')
.backgroundColor(Color.Blue)
.fontColor(Color.White)
.margin({
top: 10
})
}
.justifyContent(FlexAlign.Start)
.alignItems(HorizontalAlign.Center)
.margin({
top: 15
})
}
}

反例

import hilog from '@ohos.hilog'
@Entry
@Component
struct MyComponent{
@State message: string = '';
build() {
Column() {
Button('点击打印日志')
.onClick(() => {
this.message = 'click';
for (let i = 0; i < 10; i++) {
hilog.info(0x0000, 'TAG', '%{public}s', this.message);
}
})
.width('90%')
.backgroundColor(Color.Blue)
.fontColor(Color.White)
.margin({
top: 10
})
}
.justifyContent(FlexAlign.Start)
.alignItems(HorizontalAlign.Center)
.margin({
top: 15
})
}
}

规则集

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

Code Linter代码检查规则的配置指导请参考Code Linter代码检查