简体   繁体   English

Stenciljs @State() 不适用于 Key:Value 对

[英]Stenciljs @State() not working for Key:Value pairs

I have a Key: Value pair object in my Stenciljs project with the @State() decorator so the components re-render when one or more of the values are updated but the re-rendering does not happen.我的 Stenciljs 项目中有一个 Key: Value 对对象,带有 @State() 装饰器,因此当一个或多个值更新时组件会重新呈现,但不会发生重新呈现。 my object looks like this:我的对象是这样的:

@State() selected: {[key: string]: string} = {x: "", y: "", z: ""};

I update it the following way: this.selected['x'] = newValue;我通过以下方式更新它: this.selected['x'] = newValue;

I know the object is being updated when desired and there are no errors coming up.我知道对象在需要时正在更新,并且没有出现错误。

Any ideas on how to solve this?关于如何解决这个问题的任何想法?

Thank you谢谢

Based on Stencil docs:基于 Stencil 文档:

mutating an object will not trigger a view update in Stencil, but returning a new copy of the object will改变对象不会触发 Stencil 中的视图更新,但返回对象的新副本会

So you need to treat your selected object as immutable:因此,您需要将selected对象视为不可变的:

this.selected = {...this.selected, x: newValue};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM