Guest User

Untitled

a guest
Apr 2nd, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. useEffect(() => {
  2.  
  3. const listener = async () => {
  4.  
  5. firestore
  6. .collection('posts')
  7. .doc(slug)
  8. .collection('comments')
  9. .orderBy('createdAt', 'desc')
  10. .onSnapshot((snapshot) => {
  11. let _comments = [];
  12. snapshot.forEach(commentSnapshot => {
  13. const thisComment = commentSnapshot.data();
  14. _comments.push({commentData: thisComment, commentId: commentSnapshot.id});
  15.  
  16. setComments(_comments);
  17.  
  18. thisComment
  19. .authorRef
  20. .get()
  21. .then((snapshot) => {
  22. const { name, profilePictureURL } = snapshot.data();
  23. setAuthorInfo({name, profilePictureURL});
  24.  
  25. })
  26.  
  27.  
  28.  
  29. });
  30.  
  31. }, (error) => {
  32. console.log(error);
  33. });
  34.  
  35. console.log(comments)
  36. }
  37. listener();
  38.  
  39. listener().then(() => {
  40. setLoading(false);
  41. })
  42.  
  43. return () => listener();
  44.  
  45. }, [firestore, slug, comments]);
Advertisement
Add Comment
Please, Sign In to add comment