jsonplaceholder 데이터 조회

hanna·2023년 1월 21일
0

TIL

목록 보기
16/20

커뮤니티 게시판 구현을 위해 더미 데이터가 필요해서
https://jsonplaceholder.typicode.com/의 더미 데이터를 이용했고,
jsonplaceholder에는 posts, comments, albums, photos, todos, users 등 여러가지 데이터가 있다.

json서버를 설치해서 axios로 데이터를 받아와서 items에 담아줬다.

전체 데이터 조회

  const handleLoadAll = async (page) => {
    await axios
      .get(
        `https://jsonplaceholder.typicode.com/comments?_page=1&_limit=${limit}`
      )
      .then((res) => {
        const arr = res.data;
        // console.log(arr);
        const total = res.headers.get("x-total-count"); // 500
        setItems(arr);
      })
      .catch((err) => {
        console.log(err.response);
      });
  };

  useEffect(() => {
    handleLoadAll();
  }, []);

부분 데이터 조회

  const viewData = async () => {
    await axios
      .get(`https://jsonplaceholder.typicode.com/comments/${boardSeq}`)
      .then((res) => {
        console.log(res.data);
        setItem(res.data);
      })
      .catch((err) => {
        console.log(err.response);
      });
  };

  useEffect(() => {
    viewData();
  }, []);

잘 받아온 것을 확인할 수 있다.

0개의 댓글

관련 채용 정보