I'm trying to read the data from firebase real-time database. When I console log the snapshot, I can see that in the array that I push. But when I try to assing that to state it either returns undefined or sometimes only "No data available.". I'm pretty new to firebase so I'm really sorry if it is bad question.
//show user's liked image on the UI
export const useShowLikedImageFromDB = () => {
const [data, setData] = useState([]);
const currentUser = useAuth();
useEffect(() => {
let results = [];
const dbRef = ref(getDatabase());
return get(child(dbRef, "users/" + currentUser?.uid + "/likes"))
.then((snapshot) => {
if (snapshot.exists()) {
snapshot.forEach((item) => {
results.push(item.val());
setData((prev) => [...prev, results]);
});
console.log(data);
} else {
console.log("No data available");
}
})
.catch((error) => {
console.error(error);
});
}, []);
return { data };
};