Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the exact difference between Polymer "reflectToAttribute" and "notify"?

The Polymer properties object supports two different keys that I don't quite understand the difference between, reflectToAttribute and notify.

reflectToAttribute says that the attribute on the host node will change when the value changes.

notify says it makes the property available for two-way binding.

Aren't these sort of the same thing? If you have notify set to true, then is there any reason that you would still need reflectToAttribute?

Could someone explain to me exactly how these keys relate to one another? Thanks!

like image 316
Carson McNeil Avatar asked Jan 16 '17 06:01

Carson McNeil


1 Answers

If we "reflect" a prop, it will appear in that component's element tag as such:

<component prop></component>

As mentioned, we could use the presence of that attribute to conditionally style the component in our style section: :host([prop]) div { background-color: green }

In litElement, we simply write: reflect: true in the property definition (rather than reflectToAttribute).

As you know, if we use notify: true on a child property, then changes in its value will also occur in its corresponding parent property (2-way binding).

In litElement, notify doesn't exist. Instead, to pass info back to the parent, you can dispatch a custom event in the child and listen for it in the parent.

In Polymer, I initially thought that in order to notify a parent of a change, we had to reflect the prop as an attribute, but this isn't the case.

like image 123
mac Avatar answered Sep 27 '22 16:09

mac