Skip to content

List re-rendering causes DOM state to be lost #4291

Open
@nolanlawson

Description

@nolanlawson

Description

When using a template list (for:each or iterator:*), if that list contains other LWC components, then after a reordering of the list, it is possible for a component to be destroyed/recreated, which causes DOM state (such as focus or the play state of a <video>) to be lost.

Steps to Reproduce

For example:

    <template for:each={keys} for:item="key">
      <x-component key={key} uuid={key}></x-component>
    </template>

If the original keys is [1,2,3], and then an <input> inside of #2 is focused, and then the list is reordered to [1,3,2], then #2 will be unmounted/remounted (destroyed/recreated), causing focus inside of the <input> to be lost.

Note that this does not happen for the 1st or 3rd item in the list; it is a quirk of LWC's diffing algorithm.

Repro

Expected Results

DOM state should be kept after reordering.

Actual Results

DOM state is lost after reordering.

Possible Solution

The only way to work around this is to save the focus state outside of the component (e.g. in localStorage) when the component is unmounted and then restore it upon remount.

Also note that fixing this inside of LWC would be an observable change, so could break consumers who rely on the destroy/recreate behavior.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions