HarmonyOS 状态管理之ObjectLink的疑问

以下代码中,@ObjectLink修饰了 price 对象,而在第二个 Text 组件中,我们并不直接将 price.mPriceNormal 赋值给 Text,而是通过 getPriceText() 进行间接引用

问题:当外部改变 price 对象中的 mPriceNormal 属性时,底层框架是否会准确检测到第二个 Text 需要重绘?第一个 Text 会不会也被重绘

@Component
struct PriceWidget {
  @ObjectLink price: Price

  build() {
    Stack({ alignContent: Alignment.End }) {
      Row() {
        Text(“price”)
        Text(this.getPriceText()).fontSize(15).fontColor(this.getPriceColor())
      }
    }.width(75)
  }

  getPriceText(): string {
    let price = this.price
    if (price == null) {
      return “”
    } else if (price.mPriceNormal == null || isNaN(price.mPriceNormal) || !isFinite(price.mPriceNormal)) {
      return “”
    } else {
      return price.mPriceNormal.toFixed(this.accuracy);
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
HarmonyOS
2024-12-20 16:13:19
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

状态变量是与自定义组件绑定的,因此当状态变量改变的时整个自定义组件都会重新渲染,因此两个Text都会重新渲染

分享
微博
QQ
微信
回复
2024-12-20 19:48:07
相关问题
HarmonyOS @ObjectLink和@State相关疑问
777浏览 • 1回复 待解决
HarmonyOS 状态管理咨询
887浏览 • 1回复 待解决
HarmonyOS 关于状态管理
756浏览 • 1回复 待解决
HarmonyOS 状态管理问题
474浏览 • 1回复 待解决
web组件cookie使用
1894浏览 • 1回复 待解决
HarmonyOS状态管理框架@observed、@track
1211浏览 • 1回复 待解决
HarmonyOS ObjectLink使用
579浏览 • 1回复 待解决
HarmonyOS ObjectLink问题
586浏览 • 1回复 待解决
HarmonyOS 关于deliveryWithInstall疑问
1385浏览 • 1回复 待解决
HarmonyOS关于RichText疑问
1564浏览 • 1回复 待解决
HarmonyOS soundPool使用疑问
585浏览 • 1回复 待解决
HarmonyOS getRawFileContentSync 疑问
804浏览 • 1回复 待解决
状态管理相关问题,有人知道吗?
1070浏览 • 1回复 待解决
HarmonyOS 关于手动签名疑问
1250浏览 • 1回复 待解决
HarmonyOS 企业证书疑问
714浏览 • 1回复 待解决