[React] react-query Missing queryFn

2023. 1. 20. 20:56·🍞 FrontEnd/React

✅ 문제 상황

react-query를 사용하던 중 Missing queryFn 오류가 났다.

한 개의 게시물에 달린 댓글을 가져오는 useQuery를 작성하던 중이었다. 

import { useQuery } from "react-query";

async function fetchComments(postId) {
  const response = await fetch(
    `https://jsonplaceholder.typicode.com/comments?postId=${postId}`
  );
  return response.json();
}

export function PostDetail({ post }) {
  const { data, error, isError, isLoading } = useQuery(["post", post.id], fetchComments(post.id));

  if (isLoading) return <div>Loading...</div>;
  if (isError)
    return (
      <>
        {error.toString()}
        <div>Error...</div>
      </>
    );

  return (
    <>
      <h3 style={{ color: "blue" }}>{post.title}</h3>
      <button>Delete</button> <button>Update title</button>
      <p>{post.body}</p>
      <h4>Comments</h4>
      {data.map((comment) => (
        <li key={comment.id}>
          {comment.email}: {comment.body}
        </li>
      ))}
    </>
  );
}

 

✅ 해결 방법

 

Missing queryFn error when using useQuery

As the title suggests, I am new to using useQuery and the associated libraries, I am trying to use it to fetch data and then populates a list of fields that I have created However when I run it I get

stackoverflow.com

바로 실행해 주는 화살표를 추가해 주면 오류가 해결된다.

  const { data, error, isError, isLoading } = useQuery(["post", post.id], () =>
    fetchComments(post.id)
  );

 

오류 없이 댓글을 가져오는 것을 볼 수 있다.

저작자표시 (새창열림)

'🍞 FrontEnd > React' 카테고리의 다른 글

[React] Recoil-persist 사용해보기  (0) 2023.02.03
[React] CORS 에러 해결하기  (0) 2023.01.30
[React] react-hook-form 버전 오류  (0) 2023.01.11
[React] React-query에 대해서 알아보자  (0) 2023.01.05
[React] React-slick 캐러셀 구현 모듈  (0) 2023.01.04
'🍞 FrontEnd/React' 카테고리의 다른 글
  • [React] Recoil-persist 사용해보기
  • [React] CORS 에러 해결하기
  • [React] react-hook-form 버전 오류
  • [React] React-query에 대해서 알아보자
박빵이
박빵이
2025년에도 갓생살기
  • 박빵이
    기억보다 기록
    박빵이
  • 전체
    오늘
    어제
    • 분류 전체보기 (337)
      • 🍞 FrontEnd (97)
        • HTML+CSS (4)
        • JavaScript (17)
        • TypeScript (4)
        • React (52)
        • Next.js (2)
        • Android (15)
      • 🍞 BackEnd (24)
        • Java (15)
        • Node.js (6)
        • Spring (1)
      • 🍞 Cloud & Infra (0)
        • AWS SAA (0)
        • Microsoft Azure (0)
      • 🍞 Algorithm (147)
        • C++ (4)
        • Baekjoon (41)
        • Programmers (97)
      • 🍞 Computer Science (18)
        • 운영체제 (1)
        • 데이터 통신 (6)
        • 네트워크 (6)
        • 데이터베이스 (1)
      • 🍞 대외활동 & 부트캠프 (42)
        • 삼성 청년 SW 아카데미 (1)
        • LG유플러스 유레카 (0)
        • 한국대학생IT경영학회 (1)
        • IT연합동아리 UMC (17)
        • 길벗 블로깅 멘토 (18)
        • IT연합동아리 피로그래밍 (3)
        • 개발 컨퍼런스 (2)
  • 블로그 메뉴

    • Admin
  • 링크

    • GitHub
  • 인기 글

  • 태그

    Java
    C++
    map
    위상정렬
    react
    umc
    Android
    길벗 블로깅 멘토
    프로그래머스
    level1
    유니온파인드
    알고리즘
    길벗 블로깅 멘토링
    백준
    JavaScript
    코딩자율학습
    안드로이드
    코틀린
    Front
    level2
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
박빵이
[React] react-query Missing queryFn
상단으로

티스토리툴바