react icon indicating copy to clipboard operation
react copied to clipboard

Add key warning for TransitionGroup with single child

Open johanneslumpe opened this issue 12 years ago • 4 comments

When wrapping an item in a ReactCSSTransitionGroup and not giving it a key, upon adding/removing the item, nothing happens. The item is removed normally but no warning about a missing key is output, which is a bit confusing. Could we have a warning here, if a key is missing?

Edit: Just to clarify: The item is removed, but no transition is performed due to classes not being added

johanneslumpe avatar Jul 16 '14 18:07 johanneslumpe

:+1:

sophiebits avatar Jul 16 '14 22:07 sophiebits

@spicyj React warns about not having a key in a list of elements, but it also still tries it's best to handle the identity of each element in the list when no key is provided. Why can't the same be done with animations? Would the best solution be to warn when keys aren't present in child elements, but to also generate a key if one hasn't been provided and handle the animation?

actionnick avatar Aug 01 '14 15:08 actionnick

@nschaubeck To be clear, ReactTransitionGroup will unmount and remount a child only if the key changes – this means if you change the contents of a child without changing the key, it won't animate. If keys are omitted, React assigns sequential increasing keys to the elements, meaning that a single unkeyed child will effectively have the key 0. If you're switching between no children and one child, you'll still get an animation even if you don't specify a key but usually providing a key is what you want, which is why it might be nice to give a warning.

sophiebits avatar Aug 04 '14 05:08 sophiebits

I’m going to close since we don’t plan any more changes to TransitionGroup in React repo. Instead, it now is maintained by the community, and you can file an issue in the new repository if this is still affecting you. Thanks!

gaearon avatar Jan 27 '17 14:01 gaearon