jkomyno / react-native-user-inactivity

Simple component that alerts when the user is inactive (i.e. when the App surface hasn't been touched for X ms)
MIT License
193 stars 53 forks source link

getting incorrect values from redux inside onAction #46

Closed s-z-s closed 2 years ago

s-z-s commented 3 years ago

I'm trying to logout user after sometime when there is no item in cart (stored in redux) in my kiosk type application.

const handleActivity = (isActive: boolean) => {
      setActive(isActive);
      console.log('active: ' + isActive);
      if (!isActive && cartItems.length==0) {
            logout();
      }
}

But length is returned 0 when the function is called during first inactivity when there is an item in the cart. If the same function is called on a button press, correct value is returned. What should I do to get correct value everytime?

jkomyno commented 2 years ago

Hi, at first glance it seems that you're updating the state twice while handling your activity, which may cause odd behaviours. Try shifting line 2 down like this:

const handleActivity = (isActive: boolean) => {
      console.log('active: ' + isActive);
      if (!isActive && cartItems.length==0) {
            // update the state once such that state.isActive = isActive and a logout is performed
      }
}
jkomyno commented 2 years ago

Closed for inactivity.