diff --git a/lib/components/farcaster-data-handler.react.js b/lib/components/farcaster-data-handler.react.js --- a/lib/components/farcaster-data-handler.react.js +++ b/lib/components/farcaster-data-handler.react.js @@ -63,6 +63,15 @@ const prevCanQueryRef = React.useRef(); + // It's possible for the user to log out while handleFarcasterMutuals below + // is running. It's not a big deal, but this can lead to a useless server call + // at the end. To avoid that useless server call, we want to check whether the + // user is logged in beforehand, but the value of loggedIn bound in the + // callback will be outdated. Instead, we can check this ref, which will be + // updated on every render. + const loggedInRef = React.useRef(loggedIn); + loggedInRef.current = loggedIn; + const handleFarcasterMutuals = React.useCallback(async () => { const canQuery = isActive && !!fid && loggedIn; if (canQuery === prevCanQueryRef.current) { @@ -100,6 +109,10 @@ [(currentUserID: string)]: fid, }; + if (!loggedInRef.current) { + return; + } + void dispatchActionPromise( updateRelationshipsActionTypes, createThreadsAndRobotextForFarcasterMutuals(