Return data from Async function React Native Redux

Viewed 166

I am having trouble with accessing the data after fetching it with SecureStore in Expo for react-native.

Here is the simple code:

const infoofuser = SecureStore.getItemAsync('userInfo').then(value =>
  console.log(`this is the vlaue from infouser: ${value}`),
);
console.log(`infoouser: ${JSON.stringify(infoofuser)}`);

the first infoofuser constant definition returns the object of the intended data.

console.log(`infoouser: ${JSON.stringify(infoofuser)}`);

however returns {"_U":0,"_V":0,"_W":null,"_X":null} which U understand is a promise. I would like to simply get the data that comes from the SecureStore call and use it to set my initialState in redux.

const infoofuser = SecureStore.getItemAsync('userInfo').then(value =>
  value
);

this does not work either to access the data

1 Answers

You can use async method using async/await. Try this:

const userInfo = useSelector(state => state.userInfo);

const getData = async () => {
  try {
    const infoofuser = await SecureStore.getItemAsync('userInfo');
    console.log('infoofuser:', infoofuser)
    /// strore on redux
  } catch (err) {
     // handle error
  }
}

useEffect(() => {
  getData()
}, [])

if (!userInfo) return null

//render something else

You can check the Expo Secure Store docs for reference.

Related