webraptor / react-native-deck-swiper

tinder like react-native deck swiper
ISC License
137 stars 84 forks source link

Error: ReferenceError: componentWillUnmountAfterInteractions is not defined #84

Closed nofacez closed 2 years ago

nofacez commented 2 years ago

Issue

I was trying to implement a dynamic deck swiper using key={cards.length} approach found here, but faced a ReferenceError when cards data was updated.

Setup:

react-native-deck-swiper: 2.0.10 react-native: 0.68.0 react: 17.0.2

  <DeckSwiper
    key={cards.length}
    ref={useSwiper}
    cards={cards}
    onSwiped={onSwiped}
    backgroundColor={'transparent'}
    renderCard={renderCard}
    verticalSwipe={false}
    keyExtractor={(card) => card.id}
    cardVerticalMargin={100}
    stackSize={3}
    onSwipedLeft={onSwipeLeft}
    onSwipedRight={onSwipeRight}
    showSecondCard
    animateOverlayLabelsOpacity
    animateCardOpacity
  />

Fix

Adding this before componentWillUnmountAfterInteractions inside componentWillUnmount fixes the problem.

componentWillUnmount = () => {
    this._mounted = false;
    InteractionManager.runAfterInteractions(this.componentWillUnmountAfterInteractions.bind(this));
  }

I can introduce a PR if needed.

Thanks for your work

UPD

cardIndex trick mentioned in the docs doesn't seem to work either. Swiper doesn't rerender when new cards are added to the list. Here's what i've done:

const [currentCardIndex, setCurrentCardIndex] = useState(0);
...other functional component stuff...
  const onSwipe = (index: number) => {
    setCurrentCardIndex(index);
    if (cards.length - index < 4) {
      addMoreCards()
    }
  };
return (
  <DeckSwiper
    cardIndex={currentCardIndex}
    onSwiped={onSwipe}
  />
)

UPD2

I've searched for a possible solution in the original repo and found a working PR. For everyone who wants dynamic cards list to work, I suggest you update the package locally with the mentioned PR. If maintainer will approve this solution, I will open a PR so this feature could be added ASAP.

shiv71 commented 2 years ago

I am also getting same error when deleting the last card.

Bsingh2697 commented 2 years ago

SAME issue when changing stack

webraptor commented 2 years ago

Fixed on 2.0.11