以下demo代码中,在父组件中调用TestBuilder,传递一个string类型数据,通过TestBuilder再传递给TestComp,并更新TestComp中的UI,但实际无法刷新UI。请问中间有个builder透传的场景,如何传递数据并更新UI?
@Component
struct TestComp {
@Prop text: string = "init"
build() {
Text(this.text)
}
}
@Builder
function TestBuilder(text: string) {
Column() {
TestComp({ text: text })
}
}
@Entry
@Component
struct BuilderUpdateData {
@State message: string = 'Hello World';
build() {
Column() {
TestBuilder(this.message)
// TestComp({ text: this.message }) // 如果父组件直接调TestComp就可以刷新UI
Button('update message', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(10)
.onClick(() => {
this.message = "aaa"
})
}
.height('100%')
.width('100%')
}
}
需按引用传递参数,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builder-V5\#按引用传递参数
示例: