I'm trying to implement a function which checks if user is authenticated before rendering the main 'App' component but I can't get my code to run AFTER the promise is resolved.
class App extends Component {
render() {
const sagaMiddleware = createSagaMiddleware();
const reduxStore = createStore(reducers, {}, applyMiddleware(ReduxThunk, sagaMiddleware));
sagaMiddleware.run(Sagas)
getToken = async () => {
const token = await store.get('token')
.then( (token) => {
if (token !== null)
reduxStore.dispatch({type: LOGIN_USER});
})
.catch( (error) => console.log(error) )
}
getToken();
return (
<Provider store={reduxStore}>
<Router>
<Scene key="root">
<Scene key="login" component={ LoginPageContainer } hideNavBar={true}/>
<Scene key="feed" component={ RequireAuth(FeedPageContainer) } initial={true} hideNavBar={true}/>
</Scene>
</Router>
</Provider>
)
}
}
export default App;
The above code works perfectly fine except it renders the component before the getToken() function is completed. I think I may not be completely understanding how async/await works yet. Thanks in advance!
rendermethodgetToken()is asynchronous and returns a promise - but you're notawaiting that