I am using Reack hooks and having some trouble with asynchronous functions executing before objects are updated. My understanding of Moralis React hooks is that user
gets automatically updated after authenticate()
. I see the user object in my useEffect()
but when it returns to my handler, user
is null
function Main(props) {
const { authenticate, isAuthenticated, user } = useMoralis()
const [ authComplete, setAuthComplete ] = useState(isAuthenticated)
const onButtonSubmit = async(e) => {
e.preventDefault()
if (!isAuthenticated) {
await authenticate()
console.log(user) // returns NULL
if (authComplete === true) { // returns FALSE
validateCredentials(user)
}
}
else {
validateCredentials()
}
}
useEffect(() => {
console.log(user) // returns correct user object
if (user === null) {
setAuthComplete(false)
}
else {
setAuthComplete(true)
}
}, [user])
}