3DJakob / react-tinder-card

A npm react module for making react elements swipeable like in the dating app tinder.
https://www.npmjs.com/package/react-tinder-card
MIT License
359 stars 110 forks source link

Cannot catch useState variable inside of handle swipe function. #39

Closed nkrulikovsky closed 3 years ago

nkrulikovsky commented 3 years ago

I need to catch the useState variable inside of the handle Swipe function. Following shows my code snippet.

import { Box, Button, Typography } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";
import React, { ReactElement, useState } from "react";
import { useHistory, useRouteMatch } from "react-router-dom";
import TinderCard from "react-tinder-card";
import BookButton from "../../components/BookButton";
import CancelBtn from "../../components/CancelBtn";
import MainHeader from "../../components/MainHeader";
import { OkButton } from "../../components/OkButton";
import StyledPaper from "../../components/StyledPaper";

.......

  const [count, setCount] = useState<number>(0);
  const [like, setLike] = useState<boolean>(false);
  const history = useHistory();
  const { path, url } = useRouteMatch();

  const handleSwipe = (direction: any) => {
    console.log("console inside of funciton",count);
    setCount((prev) => prev + 1);
  };

export default function Simple(): ReactElement {
  const classes = useStyles();
  const [settlementCount, setSettlementCount] = useState<number>(0);
  const [count, setCount] = useState<number>(0);
  const [like, setLike] = useState<boolean>(false);
  const history = useHistory();
  const { path, url } = useRouteMatch();

  const handleSwipe = (direction: any) => {
    console.log("console inside of funciton",count);
    setCount((prev) => prev + 1);
  };

  console.log("this is console from outside of function", count)

  const handleTest = () => {
    console.log(count);
    setCount((prev) => prev + 1);
  };
......

      <TinderCard
              key={settlement.id}
              preventSwipe={["up"]}
              onSwipe={handleSwipe}
            >
              <StyledPaper
                style={{
                  marginTop: 10,
                  padding: "45px 10px",
                  backgroundColor: "white",
                }}
              >

......

  </TinderCard>

 );
}

Console log is following.

console inside of funciton 0 Simple.tsx:96 this is console from outside of function 1 Simple.tsx:92 console inside of funciton 0 Simple.tsx:96 this is console from outside of function 2 Simple.tsx:92 console inside of funciton 0

As you can see, the count inside of handleSwipe is always having its initial value.

Why this is? and how can I fix this problem?

Thank you.

3DJakob commented 3 years ago

Hey! This is just a result of how React works! The component that calls the callback will store the value from when it last got updated. There are probably multiple ways to solve this. Either you try to force it to update, could probably be done with some hook not sure which. Or one way to solve this is to have a global variable that you update every time you set the state and then read from that (very ugly but works). This is not a specific issue with TinderCard so you can probably find more help and solutions on other forums. Good luck!