Remove key change logic #74
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR #57 introduced some changes to ensure that children re-render when the Swiper is swiped. Specifically, it made use of
React.cloneElement
to provide each child with two new props (the currentactiveIndex
which changes on swipe, as well as the child's specificindex
). It also sets akey
prop on the children that is changed from the index to-1
for the child that becomes to currently active child (and from-1
back to the index for the child that is ceasing to be the active child).This PR removes the dynamic
key
logic. Issues #65 and #67 discuss the issues/concerns with changing thekey
on children on swipe. Specifically, changing thekey
on a child does not cause a "re-render" -- it actually causes React to fully tear down and remount the component, skipping React's diff inspection logic.As the React docs call out:
In particular, changing the
key
like this causes issues with children that contain images that may take some time to reinitialize (causing an undesired flickering effect). It also means the children will lose any internal state they may be maintaining.Passing the
activeIndex
prop (whose values changes on swipe) is sufficient to re-render the children (I verified this locally using a patched version of the library in my project with this PR's change applied). If components further down in the child tree need to re-render on swipe, then it should be the child's responsibility to incorporate logic to ensure that happens (e.g. by passing theactiveIndex
down into those sub-components). Using the childkey
change to nuke the entire child is too aggressive.That said -- if the
key
behavior is truly required/desired, I think it should be opt-in behavior via a new prop (e.g.dangerouslyRerenderChildren
) onSwiper
, rather than have it be default behavior.