Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting state in onSwipeRequirementFulfilled breaks animation in mobile browser #151

Open
youandvern opened this issue Jun 19, 2023 · 7 comments

Comments

@youandvern
Copy link

Whenever I try to call a react state setter from within the onSwipeRequirementFulfilled callback, the card animation breaks for mobile browsers. The card seems to re-render and stops being dragged. You can keep holding the screen down and the callback keeps getting called, but the card is no longer being dragged. **This is only visible on mobile browsers or devtools mobile view.

I forked the demo repo to display this bug:

@youandvern youandvern changed the title Setting state in onSwipeRequirementFulfilled broken in mobile browser Setting state in onSwipeRequirementFulfilled breaks animation in mobile browser Jun 20, 2023
@3DJakob
Copy link
Owner

3DJakob commented Sep 22, 2023

Ok, good catch! Unfortunately, I don't have time to look at this right now but PRs are accepted.

@ikesler
Copy link

ikesler commented Mar 1, 2024

As a workaround - put TinderCard in React's useMemo. Working flawlessly for me. Btw, the issue happened to me also in desktop.

@anonimitoraf
Copy link

Hey @ikesler do you mind elaborating or giving a snippet of how you did it?

@ikesler
Copy link

ikesler commented Jun 18, 2024

Hey @ikesler do you mind elaborating or giving a snippet of how you did it?

The code was a bit specific to my project, here is the simplified version:

  const MemoizedCard = useMemo(() => (
    <VerseSwipeCardInternal {...myPropsObj} />
  ), [...myPropsArray]);
const VerseSwipeCardInternal: FC<VerseSwipeCardInternalProps> = (props) => {
...
  return <TinderCard
...
}

And yes - we've switched to entirely different UX eventually, so I do not use swipes anymore

@anonimitoraf
Copy link

Ah, thanks for sharing

@AndersDJohnson
Copy link

No worries if no time to address - but noting that I'm experiencing the same issue. And I'm not sure memoizing will help, since I'd like to be able to change the internal state of the card to color it according to swipe left or right.

@AndersDJohnson
Copy link

No worries if no time to address - but noting that I'm experiencing the same issue. And I'm not sure memoizing will help, since I'd like to be able to change the internal state of the card to color it according to swipe left or right.

For what it's worth, I was able to work around this for now by memoizing the <TinderCard> JSX as suggested above, plus adding a custom class name to a wrapper <div> according to my swipe direction - i.e., swipe-direction-left or swipe-direction-right - and then having the internals of my cards react to that class by nesting rules under one or the other parent CSS selector, e.g.:

.swipe-direction-right .my-card-background {
  background-color: green;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants