This is my code
useEffect(() => {
console.log('useEffect')
let database = firebase.database();
database.ref().on('value', (snapshot) => {
console.log('hit');
setQuestions(snapshot.val());
console.log('questions here ', questions)
})
}, []);
console.log(questions);
The output is
undefined
useEffect
undefined
hit
(15) [{...}] //This is questions being logged outside useEffect
questions here undefined
The console.log outputs are all over the place and aren't in order. Which I assume is because js is executed asynchronously. But what I don't get at all is how is the console.log outside useEffect logging questions
as (15) [{...}]
but then the console.log in useEffect, which is executed after logs questions
as undefined. Is there a way I can fix this and make questions equal the array of objects when its logged in useEffect?