Retrieve value from SecureStore in main App function

Viewed 144

I want to retrieve a value from SecureStore in the main App and send the user to the Settings screen if not already set.

I have taken the get value function from the Expo documentation (except I've added a return):

async function getValueFor(key) {
  let result = await SecureStore.getItemAsync(key);
  if (result) {
    alert(" Here's your value  \n" + result);
  } else {
    alert('No values stored under that key.');
  }
  return result; 
}

Then in the App:

export default function App() {
  const url = getValueFor('url');
  console.log('url set to ' + url);
  var initialRoute = 'Home';
  if (url == null) {
    initialRoute = 'Settings';
    console.log('Initial route set to Settings');
  }
  return (
    <PaperProvider>
    <NavigationContainer>
      <Stack.Navigator initialRouteName={initialRoute}
      screenOptions={{
        header: (props) => <CustomNavigationBar {...props} />,
      }}>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Process" component={ProcessScreen} />
        <Stack.Screen name="Settings" component={settings} />
      </Stack.Navigator>
    </NavigationContainer>
    </PaperProvider>
  );
}

But of course the getValueFor function being async returns a Promise rather than the key value. I can't use an await in App() as it can't be async and also I can't move everything into a 'then' as it says 'Nothing was returned from render'.

I have looked at the various questions on stackoverflow regarding SecureStore.getItemAsync but can't see one that would make a value available in the main App function.

0 Answers
Related