@State变量在子组件中修改后父组件不更新,怎么解决?
鸿蒙6.0ArkUI
我使用state声明的变量,子组件也是state,修改子组件的变量时,为什么父组件不刷新,如果不使用link,怎么刷新
您需要先 登录 才能评论/回答
全部评论(1)
建议使用callback实现:
@Component
struct A {
@State testValA: number = 0;
callBack: (count: number) => void = (count: number) => {
this.testValA = count;
};
build() {
Column() {
Text(`${this.testValA}`);
B({ testValB: this.testValA, callBack: this.callBack });
};
}
}
@Component
struct B {
callBack?: (count: number) => void;
@State testValB: number = 1;
build() {
Button().onClick(() => {
this.testValB = 2; // 单单这样写是不会生效的
this.callBack && this.callBack(this.testValB); // 这样写可以生效
});
}
}
2026-01-27 21:38:19