Last active
February 27, 2022 06:44
-
-
Save amandeepmittal/df20505b05c6911b57b01575a22c586d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useEffect(() => { | |
const collectionRef = collection(database, 'chats'); | |
const q = query(collectionRef, orderBy('createdAt', 'desc')); | |
const unsubscribe = onSnapshot(q, querySnapshot => { | |
setMessages( | |
querySnapshot.docs.map(doc => ({ | |
_id: doc.data()._id, | |
createdAt: doc.data().createdAt.toDate(), | |
text: doc.data().text, | |
user: doc.data().user | |
})) | |
); | |
}); | |
return () => unsubscribe(); | |
}, []); | |
const onSend = useCallback((messages = []) => { | |
setMessages(previousMessages => | |
GiftedChat.append(previousMessages, messages) | |
); | |
const { _id, createdAt, text, user } = messages[0]; | |
addDoc(collection(database, 'chats'), { | |
_id, | |
createdAt, | |
text, | |
user | |
}); | |
}, []); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useLayoutEffect(() => { | |
const collectionRef = collection(database, 'chats'); | |
const q = query(collectionRef, orderBy('createdAt', 'desc')); | |
const unsubscribe = onSnapshot(q, querySnapshot => { | |
setMessages( | |
querySnapshot.docs.map(doc => ({ | |
_id: doc.data()._id, | |
createdAt: doc.data().createdAt.toDate(), | |
text: doc.data().text, | |
user: doc.data().user | |
})) | |
); | |
}); | |
return unsubscribe; | |
}); | |
const onSend = useCallback((messages = []) => { | |
setMessages(previousMessages => | |
GiftedChat.append(previousMessages, messages) | |
); | |
const { _id, createdAt, text, user } = messages[0]; | |
addDoc(collection(database, 'chats'), { | |
_id, | |
createdAt, | |
text, | |
user | |
}); | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment