Skip to content

Commit

Permalink
feature: use fetch hook success handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnsonMao committed Mar 17, 2024
1 parent bb2eb89 commit cabe95e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion hooks/useFetch.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useReducer, useState } from 'react';

const useFetch = (url, { initialValue } = {}) => {
const useFetch = (url, { initialValue, onSuccess } = {}) => {
const [render, refetch] = useReducer((pre) => !pre, true);
const [data, setData] = useState(initialValue);
const [isFetching, setIsFetching] = useState(true);
Expand All @@ -25,6 +25,10 @@ const useFetch = (url, { initialValue } = {}) => {
};
}, [url, render]);

useEffect(() => {
if (onSuccess) onSuccess(data);
}, [onSuccess, data]);

return { data, isFetching, isError, refetch };
};

Expand Down

0 comments on commit cabe95e

Please sign in to comment.