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

Not getting updated values from store inside onAction #54

Open
nbAmit opened this issue Apr 19, 2022 · 2 comments
Open

Not getting updated values from store inside onAction #54

nbAmit opened this issue Apr 19, 2022 · 2 comments

Comments

@nbAmit
Copy link

nbAmit commented Apr 19, 2022

I'm trying to logout user he/she us inactive for x number of minutes, I have two different different scenarios

  1. Onboarding screen before login/register
  2. Screens after login

So I am checking here if user is inactive on any of the Onboarding screens then I have to restart the Onboarding flow, If user login into the app then I have show the idle screen. For checking this status I am using the react native store but when onAction method call it always return the old store.

const performAutoLogout = async () => {
  if (!store.isUserLogin) {
    navigation.reset({
      index: 0,
      routes: [
        {
          name: 'Get Started',
        },
      ],
    })
  } else {
    navigation.reset({
      index: 0,
      routes: [
        {
          name: 'App Idle',
        },
      ],
    })
  }
}
return (
    <UserInactivity
      isActive={false}
      timeForInactivity={authTimeout}
      onAction={(isActive: boolean) => {
        // if timer exceeds x min then isActive = false and logout user
        if (!isActive) {
          performAutoLogout()
        }
      }}
    >
      <Stack.Navigator>
        {store.isUserLogin ? (
          <Stack.Group>
            <Stack.Screen name="Tabs">{() => <TabStack />}</Stack.Screen>
            <Stack.Screen name="Settings">{() => <SettingStack />}</Stack.Screen>
            <Stack.Screen name="Notifications" component={Notifications} />
            <Stack.Screen name="Idle Session" component={IdleSession} initialParams={{ setAuthenticated }} />
          </Stack.Group>
        ) : (
          <Stack.Group>
              <Stack.Screen name="Get Started" component={GetStarted} />
              <Stack.Screen name="Onboarding Screen" component={OnboardingScreen} />
              <Stack.Screen name="Terms" component={Terms} />
             <Stack.Screen name="Conditions" component={Conditions} />
          </Stack.Group>
        )}
      </Stack.Navigator>
    </UserInactivity>
  )
@XxLuisFer15xX
Copy link

XxLuisFer15xX commented Apr 29, 2022

You can try saving and getting the data with async storage.
It's not the best way but it works for now.

const detectIdle = async activeState => {
  const isLogin = await stgGetIsLogin();
  if (isLogin && !activeState) {
    _logOut();
  }
};
<UserInactivity
  isActive={isActive}
  timeForInactivity={activityTime}
  onAction={detectIdle}>
  ...
</UserInactivity>

@abdemirza
Copy link

You can create a copy of the state into useRef and then pass it, worked for me.

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

3 participants