React Native Proper Initial Redux(with AsyncStorage) States

Viewed 23

I'm trying to be able to start up my App as either going unto Homescreen if logged in or to Sign Up screen if not using Redux Initial state and also Async Storage.

I have below code but everytime I try to access the userInfo in main App it always signs in my App and goes directly to homescreen even if no log in sessions has been done yet.

Surprisingly, I tried to log the initial userInfo which should be null if accessed from the AsyncStorage from the initial state but it shows below. It seems AsyncStorage creates this Object hence the App thinks that userInfo is not null. {"_1": 0, "_2": 1, "_3": undefined, "_4": null}

Do appreciate any replies/help. Thanks in advance.

import AsyncStorage from '@react-native-async-storage/async-storage';
import thunk from 'redux-thunk'
import { composeWithDevTools } from '@redux-devtools/extension';
import { userSignUpReducer, userSignInReducer }from './reducers/userReducers';

const getData = async (key) => {
let value;
try {
    if(key=== 'userInfo'){
     value = await AsyncStorage.getItem(key) ? JSON.parse(AsyncStorage.getItem(key)) : null
     console.log(value)
     return
    }
    value = await AsyncStorage.getItem(key) ? JSON.parse(AsyncStorage.getItem(key)) : []
} catch (error) {
 console.log(error); 
}
  return value
};

const reducer = combineReducers({
  userSignIn: userSignInReducer,
  userSignUp: userSignUpReducer,
})

const initialState = {
   userSignIn: { userInfo: getData('userInfo')}
};

const configureStore = () => {
    return createStore(reducer,initialState,composeWithDevTools(applyMiddleware(thunk)));
}


export default configureStore; 
0 Answers
Related