I'm using React 16. I have a hook where I determine if a session token has been stored in session storage ...
import { useEffect } from 'react';
import { useAuthenticationState, useAuthenticationDispatch } from '../context';
const useAuthentication = () => {
const authenticationState = useAuthenticationState();
const updateAuthenticationState = useAuthenticationDispatch();
useEffect(() => {
const auth_token = sessionStorage.getItem('token');
console.log("auth token: " + auth_token);
updateAuthenticationState({
type: 'field',
fieldName: 'isAuthenticated',
payload: !!auth_token,
});
}, [updateAuthenticationState]);
const isAuthenticated = authenticationState.isAuthenticated;
return {
isAuthenticated,
};
};
export default useAuthentication;
I would like to pass the value of what is stored in session storage to a component that will either render another component or redirect based on the value of the my hook ...
const DirectoryApp = () => {
console.log("starting get hook value ...");
const { isAuthenticated } = useAuthentication();
console.log("is auth:" + isAuthenticated);
return (
<Router>
...
<PrivateRoute
authed={isAuthenticated} path="/unapproved-list/"
component={UnapprovedList}
/>
But this line
const { isAuthenticated } = useAuthentication();
isn't properly getting the value of what is in sessionStorage -- it is always initialy returning false. I think this is because I'm not awaiting the result of what the hook returns, but if I do this
const { isAuthenticated } = await useAuthentication();
I get the error
Syntax error: Unexpected reserved word 'await'. (24:31)
How do I properly await the value of what the hook returns?