Reactjs Can';获取数据时,不要对未安装的组件错误执行React状态更新

Reactjs Can';获取数据时,不要对未安装的组件错误执行React状态更新,reactjs,google-cloud-firestore,firebase-authentication,react-hooks,use-effect,Reactjs,Google Cloud Firestore,Firebase Authentication,React Hooks,Use Effect,我试图获取一些数据时遇到问题。出于某种原因,我不断收到此错误: Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup

我试图获取一些数据时遇到问题。出于某种原因,我不断收到此错误:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
这是整个组件,我在其中获取数据,然后将其传递到DroperConfig组件。然后,这些数据会被传递到其他组件:

export default function Root() {
  const [userImage, setUserImage] = useState();
  const [userName, setUserName] = useState();
  const [name, setName] = useState();
  const [urlName, setUrlName] = useState();
  const [userID, setUserID] = useState();
  const [followerCount, setFollowerCount] = useState();
  const [followingCount, setFollowingCount] = useState();

  const [links, setLinks] = useState([{link: null}]);

  const [pageLoading, setPageLoading] = useState(true);

  // Following counts, displayname, image
  const fetchData = useCallback((data) => {
    const dataRef = firestore().collection('usernames');
    const usersLinks = firestore().collection('links');

    // Fetch user Links
    usersLinks.doc(data.data().urlName).onSnapshot((doc) => {
      const entries =
        doc.data() === undefined ? [undefined] : Object.values(doc.data());
      entries[0] === undefined ? setLinks([{link: null}]) : setLinks(entries);
    });

    dataRef.doc(data.data().urlName).onSnapshot((snap) => {
      // Fetch users image
      setUserImage(snap.data().imageUrl);
      setUserID(snap.data().displayName);
      setUserName(snap.data().userName);
      setUrlName(data.data().urlName);
      setName(snap.data().displayName);
      setFollowerCount(snap.data().followers);
      setFollowingCount(snap.data().following);
      setPageLoading(false);
    });
  }, []);

  // Fetch all data here
  useEffect(() => {
    auth().onAuthStateChanged((user) => {
      if (user !== null) {
        if (user.emailVerified) {
          const cleanup = firestore()
            .collection('users')
            .doc(user.uid)
            .onSnapshot(fetchData);

          return cleanup;
        }
      }
    });
  }, [fetchData]);

  return (
    <>
      {/* ALL SCREENS */}
      {pageLoading ? (
        <ActivityIndicator size="large" color="black" />
      ) : (
        <DrawerConfig
          links={links}
          username={userName}
          userimage={userImage}
          userid={userID}
          displayname={name}
          urlname={urlName}
          followerCount={followerCount}
          followingCount={followingCount}
        />
      )}
    </>
  );
}
导出默认函数根(){
const[userImage,setUserImage]=useState();
const[userName,setUserName]=useState();
const[name,setName]=useState();
const[urlName,setUrlName]=useState();
const[userID,setUserID]=useState();
const[followerCount,setFollowerCount]=useState();
const[followCount,setfollowCount]=useState();
const[links,setLinks]=useState([{link:null}]);
const[pageLoading,setPageLoading]=useState(true);
//以下计数、显示名称、图像
const fetchData=useCallback((数据)=>{
const dataRef=firestore().collection('usernames');
const usersLinks=firestore().collection('links');
//获取用户链接
usersLinks.doc(data.data().urlName).onSnapshot((doc)=>{
常量条目=
doc.data()==未定义?[未定义]:对象值(doc.data());
条目[0]==未定义的setLinks([{link:null}]):setLinks(条目);
});
dataRef.doc(data.data().urlName).onSnapshot((snap)=>{
//获取用户图像
setUserImage(snap.data().imageUrl);
setUserID(snap.data().displayName);
setUserName(snap.data().userName);
setUrlName(data.data().urlName);
setName(snap.data().displayName);
setFollowerCount(snap.data().follower);
setFollowCount(snap.data().following);
设置页面加载(假);
});
}, []);
//在这里获取所有数据
useffect(()=>{
auth().onAuthStateChanged((用户)=>{
如果(用户!==null){
if(user.emailVerified){
const cleanup=firestore()
.collection('用户')
.doc(user.uid)
.onSnapshot(获取数据);
回流清理;
}
}
});
},[fetchData]);
返回(
{/*所有屏幕*/}
{页面加载(
) : (
)}
);
}

任何帮助都将不胜感激,谢谢

看起来您需要稍微修改一下useEffect-我不认为您卸载此组件时您的侦听器被取消订阅

  // Fetch all data here
  useEffect(() => {
    return auth().onAuthStateChanged((user) => {
    ...
    })
  })

.onAuthStateChanged()返回unsubscribe函数;useEffect接受一个unsubscribe函数作为卸载时执行的返回。

仅凭这段代码很难判断。我还能提供什么?谢谢展示整个组件。或者,更好的办法是,将其放入沙箱中。我已经更新了我的问题,以显示整个组件。您是否在加载或执行特定操作时看到警告?这似乎已经解决了问题。谢谢你,老板!