Repository navigation
Changing x-data attribute value on an existing element does not update Alpine's internal state
#4758
Replies: 2 comments 5 replies
|
This is mostly intended, since it's impossible for Alpine to really know what values you would want to update, and which you might want to stay the same (like if some changed at runtime?) The whole idea of If this is to be supported, I think none of those solutions (that are in Alpine's control) would be appropriate. The idea would be to just evaluate the x-data and apply it's data onto the existing state. |
5 replies
|
This has now been resolved in #4868 as this change now handles updating x-data attribute properly for livewire morphed changes and so will resolve htmx morph as well. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Description
When an element's
x-dataattribute is updated in-place (e.g. via htmx morph), Alpine does not apply the new data scope. The element retains its old state instead of reinitializing with the newx-datavalue.Root Cause
Once an element is initialized by Alpine,
_x_markeris set on it. Whenx-datais changed, Alpine's mutation observer treats this as a remove + add of the attribute and calls thex-datacleanup function. However, this cleanup does not clear_x_markeror other internal Alpine state properties. When Alpine then detects the attribute being "added", it sees_x_markerand skips initialization, leaving the element with its old data scope.Steps to Reproduce
x-data="{ count: 1 }"x-data="{ count: 99 }"count: 1Expected: Alpine reinitializes the element with the new
x-datavalue.Actual: Alpine retains the old data scope because
_x_markerblocks reinitialization.Possible Solutions
External workaround (htmx extension): Detect
x-dataattribute changes after morph completes and manually callAlpine.destroyTree()+Alpine.initTree()on the affected element.onAttributeChangedhook: Add a mutation handler inside Alpine that detectsx-dataattribute changes and triggers a full de-init/re-init cycle. (branch)Fix the
x-datacleanup function: Update the existing cleanup to perform a full teardown of the element and its children (clearing_x_markerand related state), then callinitTreeto reinitialize. (branch)All reactions