HarmonyOS 自定义builder方法, 参数是按引用传递, 当状态值修改时, 不触发builder方法, 不会刷新UI

点击clickme时, 希望text的值变为overBuilder===ArkUI。

@Builder  
function overBuilder($$: string) {  
  Row() {  
    Column() {  
      Text(`overBuilder===${$$}`)  
    }  
  }  
}  
  
@Entry  
@Component  
struct Parent {  
  @State label: string = 'Hello';  
  
  build() {  
    Column() {  
      Button('Click me').onClick(() => {  
        this.label = 'ArkUI';  
      })  
      overBuilder(this.label)  
    }  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
HarmonyOS
2024-09-27 11:25:25
1190浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

demo如下:

class Tmp {  
  paramA1: string = ''  
}  
  
@Builder function overBuilder(params: Tmp) {  
  Row() {  
    Text(`UseStateVarByReference: ${params.paramA1} `)  
  }  
}  
@Entry  
@Component  
struct Parent {  
  @State label: string = 'Hello';  
  build() {  
    Column() {  
      // Pass the this.label reference to the overBuilder component when the overBuilder component is called in the Parent component.  
      overBuilder({ paramA1: this.label })  
      Button('Click me').onClick(() => {  
        // After Click me is clicked, the UI text changes from Hello to ArkUI.  
        this.label = 'ArkUI';  
      })  
    }  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
分享
微博
QQ
微信
回复
2024-09-27 17:13:57
相关问题
HarmonyOS @builder方法ui刷新
1227浏览 • 1回复 待解决
HarmonyOS builder 作为 builder参数传递
911浏览 • 1回复 待解决
HarmonyOS @builder引用传递问题
1278浏览 • 0回复 待解决
HarmonyOS @Builder UI刷新问题
741浏览 • 1回复 待解决
js 自定义组件如何传递方法
6735浏览 • 2回复 待解决