const [user] = useAuthState(auth);
const [ChatList, setChatList] = useState([]);
useEffect(() => {
const ChatCollectionRef = collection(db, "ChatList");
const queryChat = query(
ChatCollectionRef,
where("usernames", "array-contains", user.displayName),
orderBy("createdAt")
);
const unsuscribe = onSnapshot(queryChat, (snapshot) => {
let chat = [];
snapshot.forEach((doc) => {
chat.push({ ...doc.data(), id: doc.id });
});
setChatList(chat);
});
return () => unsuscribe();
}, []);
#User.uid is null even though user is logged in and when I log it to the console it returns the id
49 messages · Page 1 of 1 (latest)
Not sure we have enough to go on here. Where are you logging these values? I only see user.displayName in use in this code. If user starts off without a value, then your useEffect will retain that as a stale closure since your dependency is missing. Honestly could be anything though, hard to pin point with such a small example.
it's actually like this
import React from "react";
import { db, auth } from "../firebase";
import {
collection,
addDoc,
serverTimestamp,
where,
getDocs,
query,
onSnapshot,
orderBy,
} from "firebase/firestore";
import { Link } from "react-router-dom";
import { useAuthState } from "react-firebase-hooks/auth";
import { useState, useEffect } from "react";
const Inbox = () => {
const [user] = useAuthState(auth);
const [ChatList, setChatList] = useState([]);
useEffect(() => {
const ChatCollectionRef = collection(db, "ChatList");
const queryChat = query(
ChatCollectionRef,
where("users", "array-contains", user.uid)
);
const unsuscribe = onSnapshot(queryChat, (snapshot) => {
let chats = [];
snapshot.forEach((doc) => {
chats.push({ ...doc.data(), id: doc.id });
});
setChatList(chats);
});
return () => unsuscribe();
}, []);
console.log(ChatList);
return <div>{ChatList}</div>;
};
export default Inbox;
if I add user?.id it says cant send undefined value to firebase
In this uid returns undefined
const [user] = useAuthState(auth);
const [ChatList, setChatList] = useState([]);
console.log(user);
What do you get on your renders like this? Does it start undefined and then get a value after a render or two?
so never undefined? can you show the whole object there?
yeah
Should I use this outside of useEffect and put it on an onClick?
Or is there a way to get it working
but the value is null there?
Nope
useEffect(() => {
console.log(user);
const ChatCollectionRef = collection(db, "ChatList");
const queryChat = query(
ChatCollectionRef,
where("users", "array-contains", user.uid)
);
const unsuscribe = onSnapshot(queryChat, (snapshot) => {
let chats = [];
snapshot.forEach((doc) => {
chats.push({ ...doc.data(), id: doc.id });
});
setChatList(chats);
});
return () => unsuscribe();
}, [])
Can you remove the other user log and try this one?
and show me?
OK what if you do
useEffect(() => {
if(!user) return;
const ChatCollectionRef = collection(db, "ChatList");
const queryChat = query(
ChatCollectionRef,
where("users", "array-contains", user.uid)
);
const unsuscribe = onSnapshot(queryChat, (snapshot) => {
let chats = [];
snapshot.forEach((doc) => {
chats.push({ ...doc.data(), id: doc.id });
});
setChatList(chats);
});
return () => unsuscribe();
}, [user])
yeas i expect its your initial value and the stale closure
Why does react log it 2 times?
Because with Strict mode enabled they are running effects twice
and this is good
because it will find bugs due to poor cleanup
I would reccomend personaly, you look into https://github.com/FirebaseExtended/reactfire
Yeah or even better. Skip Firebase completely.