1 year ago

#74281

test-img

Süleyman Şentosun

How do I avoid unnecessary setState usage when receiving real-time user posts from Cloud Firestore?

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?

reactjs

google-cloud-firestore

use-effect

feed

real-time-updates

0 Answers

Your Answer

Accepted video resources