IONIC / REACT 行 9:5:期望赋值或函数调用,但看到表达式 @typescript-eslint/no-unused-expressions

IONIC / REACT Line 9:5: Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions

当我尝试制作钩子以从 firebase 获取用户数据时。像这样

import { useEffect, useState } from 'react';
import { firestore } from '../../'
import { getAuth } from 'firebase/auth';
export function useUserDetails() {
  const db = firestore;
  const userid = getAuth().currentUser?.uid
  const [userDetails, setuserDetails] = useState<unknown>([])
  useEffect(() => {
    (async () => {
     const doc = await db.collection("Users").doc(userid).get();
      setuserDetails(({...doc.data(), id: doc.id}))
  })}, [])
  return [userDetails]
}

向我反应 return 一个错误:第 9:5 行:需要赋值或函数调用,却看到表达式 @typescript-eslint/no-unused-expressions.

您需要在useEffect 中调用匿名函数。看起来您的 setuserDetails 调用中有一组额外的括号。另外,doc.data()doc.id 都正确吗?

useEffect(() => {
  (async () => {
    const doc = await db.collection("Users").doc(userid).get();
    // removed parens
    setuserDetails({...doc.data(), id: doc.id}));
  // added parens
  })();
}, []);

已修正:这是使用 Effect 的正确方法

useEffect(() => {
    (async function() {
      const doc = await db.collection("Users").doc(userid).get();
      setuserDetails(({...doc.data(), id: doc.id}))
    })();
}, []);