jkomyno/react-native-user-inactivity

getting incorrect values from redux inside onAction

s-z-s opened this issue · 2 comments

s-z-s commented

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?

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
      }
}

Closed for inactivity.