1

I structured the feed and follow system according to the answer in the link below.

https://stackoverflow.com/a/52153332/11427790

However, I have to use unnecessary setFeed method when receiving posts from a particular user's followers.

  useEffect(() => {
    function handleFollowingChange(userFollowing) {
      const feedTweets = {};
      userFollowing.forEach((userDoc) => {
        const queryForUserTweets = query(collection(db, "tweets/" + userDoc.id + "/userTweets"));
        const unsubscribe = onSnapshot(queryForUserTweets, (userTweets) => {
          feedTweets[userDoc.id] = userTweets;
          setFeed(feedTweets);
        });
      });
    }

    const queryForFollowing = query(collection(db, "following/" + auth.currentUser.uid + "/userFollowing"));
    const unsubscribe = onSnapshot(queryForFollowing, handleFollowingChange);
  }, []);

The reason for this is that when there is an update in the posts I am interested in, I need to change the feed state and therefore use the setFeed method, whereas it is unnecessary to run setState for each user in the initial callback to collect the posts I am interested in.

How can I overcome this problem?

  • You may have a look at this [Stackoverflow case](https://stackoverflow.com/questions/57508459/cant-setstate-firestore-data). Let me know if that helps! – Mousumi Roy Jan 21 '22 at 08:52

0 Answers0