amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Any update on amp-state will trigger global re-render

Open AndrewKGuan opened this issue 8 years ago • 6 comments

Whenever there's an update to amp-state, all amp-bound elements on the page will get re-evaluated and re-rendered, even when the elements are not bound to the updated amp-state variables.

One simple example here: http://testmip.herokuapp.com/triplelinkeddropdown.html

The second drop down does not retain selected value because it's been re-rendered after a setState triggered by itself, and the variable being set is not the one bound to the drop down.

I have some other more complicated examples (not yet available publicly) that also exhibit the same behavior.

AndrewKGuan avatar Mar 20 '18 20:03 AndrewKGuan

A work-around for this behavior is to store and set the selected state via amp-bind. This makes things unnecessarily complicated though.

sebastianbenz avatar Mar 21 '18 10:03 sebastianbenz

We actually currently do this but only check recursive equality of arrays/objects to a depth of 3 (which falls just short of your use case). I think we can bump it up safely.

dreamofabear avatar Mar 22 '18 16:03 dreamofabear

Hi William, would this change help prevent any amp-bound template-generated elements from overwritten? For example, a form with a combination of multiple inputs textboxes and a group of radio buttons (observing tap event) dynamically generated from amp-state json, will tapping on the radio buttons wipe out texts a user entered in those textboxes?

AndrewKGuan avatar Apr 03 '18 00:04 AndrewKGuan

Yes, but only if the output of the template render is identical. We haven't implemented diffing of rendered templates yet, e.g. only append new elements instead of re-rendering everything.

Would that be useful for your use case?

dreamofabear avatar Apr 03 '18 17:04 dreamofabear

Hello,

Has this regressed. If I look at the amp.dev playground example,

https://playground.amp.dev/?url=https://preview.amp.dev/documentation/examples/components/amp-bind

Clicking on any of the buttons causes all the state across the page to be re-evaluated. Should this not be scoped to a given action alone?

shishirm avatar Jun 05 '19 18:06 shishirm

@shishirm This was always the case. You can use default values in expressions to avoid this behavior e.g. [text]="foo || 'default text'".

dreamofabear avatar Jun 05 '19 18:06 dreamofabear