简体   繁体   中英

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

When i try to make hook to get userdata from firebase. Like this

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]
}

react return me a error : Line 9:5: Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions.

You need to invoke the anonymous function in your useEffect. It looks like you have an extra set of parens in your setuserDetails call. Also, are doc.data() and doc.id both correct?

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

Fixed: This is correct way to useEffect

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM