Last active
May 31, 2023 15:11
-
-
Save alperkilickaya/52cd9cbc84d5899e7c155baa7c66008f to your computer and use it in GitHub Desktop.
React Custom Http Hook with AbortController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState, useCallback, useRef, useEffect } from "react"; | |
| export const useHttpClient = () => { | |
| const [isLoading, setIsLoading] = useState(false); | |
| const [error, setError] = useState(); | |
| // bu hook'un kullanıldığı component her render olduğunda aynı kalacak bir değişken oluşturur. | |
| const activeHpptRequests = useRef([]); | |
| const sendRequest = useCallback( | |
| async (url, method = "GET", body = null, headers = {}) => { | |
| setIsLoading(true); | |
| //her istek atıldığında kullanmak üzere bir AbortController oluşturuyoruz. | |
| const httpAbortCtrl = new AbortController(); | |
| activeHpptRequests.current.push(httpAbortCtrl); | |
| try { | |
| const response = await fetch(url, { | |
| method, | |
| body, | |
| headers, | |
| signal: httpAbortCtrl.signal, | |
| }); | |
| const responseData = await response.json(); | |
| // istek atıldıktan sonra, istekler dizisinden bu isteğin AbortController'ını çıkarıyoruz. | |
| activeHpptRequests.current = activeHpptRequests.current.filter( | |
| (reqCtrl) => reqCtrl !== httpAbortCtrl | |
| ); | |
| if (!response.ok) { | |
| throw new Error(responseData.message); | |
| } | |
| setIsLoading(false); | |
| return responseData; | |
| } catch (err) { | |
| setError(err.message || "Something went wrong, please try again."); | |
| setIsLoading(false); | |
| // eğer custom hook içinde bir error oluşursa, bu error'u dışarıya fırlatıyoruz. aksi halde kullanılan component'te kod execution devam eder. | |
| throw err; | |
| } | |
| }, | |
| [] | |
| ); | |
| const clearError = () => { | |
| setError(null); | |
| }; | |
| //bu hook kullanıldığı component unmount olduğunda çalışır. Bu sayede component unmount olmadan önceki istekler iptal edilir ve memory leak oluşmaz. | |
| useEffect(() => { | |
| return () => { | |
| activeHpptRequests.current.forEach((abortCtrl) => abortCtrl.abort()); | |
| }; | |
| }, []); | |
| return { isLoading, error, sendRequest, clearError }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment