react-query - useLazyQuery 사용방법

sangwoo noh·2022년 7월 26일
0

ReactJS

목록 보기
12/16
  • react-query에 uselazy query라는 동작은 없지만...
  • apollo의 그것과 같은 동작을 원한다...
  // setEmailText이 trigger이다. 이것을 원하는 곳에서 사용하여 useQuery를 작동 시킨다.
  
  
  const serachEmail = async <
    T extends string,
    U extends string,
    V extends number,
  >(
    emailTextSE: T,
    dbnameTextSE: U,
    offsetSE: V,
  ) => {
    console.log('emailTextSE', Boolean(emailTextSE))
    if (!emailTextSE) return    // 여기서 emailText가 빈칸일땐 동작하지 않도록 early return 해준다

    return findManyEmailList({
      email: emailTextSE,
      dbname: dbnameTextSE,
      offset: offsetSE,
    })
  }
  
  
 const [emailText, setEmailText] = useState('')
 
  const {
    data: getEmailList,
    refetch,
    isLoading: isFindMayEmailLoading,
  } = useQuery(['findManyEmail', emailText], () =>
    serachEmail(emailText, dbnameText, offset),    // some fetching function..
  )

  // HACK
  useEffect(() => {
    if (!!emailText) {
      refetch()
    }
  }, [emailText])
  
profile
하기로 했으면 하자

0개의 댓글