Skip to content

When updating from many-item to single-item array, re-render is forced although item key hasn't changed #1493

Closed
@gaearon

Description

@gaearon

Consider this:

render: function () {
  return <div>{arr.map(this.renderItem)}</div>;
}

Usually, key is respected in this case and elements don't get unmounted and remounted unnecessarily if their keys match between subsequent render calls.

However, if all elements but one are removed, React 0.10.0 for some reason doesn't realize it's the same component (the key hasn't changed) and forces its DOM node to update.

I tried setting a breakpoint inside shouldUpdateReactComponent and _updateChildren to debug the issue and it looks like prevComponentInstance is null because its name in nextChildren and prevChildren is slightly different (in one case it includes leading 0:, in the other it doesn't).

Anyway, the workaround for me was, curiously, not returning an array when there is just one item, and rendering it as is. In this case, React was able to understand it's the same component, and didn't force an unnecessary re-render.

I'm sorry for not providing a workable fiddle or something—jsFiddle hosts an old version of React, and it's 8am where I live so I should go to sleep. This should be fairly simple to reproduce, unless it's already fixed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions