매일매일
Custom Hook
React 2023. 3. 27. 21:51

useFetch fetch 할 때 반복되는 코드를 커스텀 훅으로 만들어 활용할 수 있다. import { useState, useEffect } from 'react'; const useFetch = (url) => { const [data, setData] = useState(null); const [isPending, setIsPending] = useState(true); const [error, setError] = useState(null); useEffect(() => { fetch(url, { // get의 경우 헤더를 작성하지 않아도 괜찮 headers: { "Content-Type" : "application/json", Accept: "application/json" } }) .then..