Fakultas Ilmu Komputer UI

Commit bbd2bbfa authored by wulanmantiri's avatar wulanmantiri
Browse files

[REFACTOR] Modify logic for useApi hooks to minimize render

parent 62921390
Pipeline #76384 passed with stages
in 26 minutes and 50 seconds
......@@ -8,16 +8,14 @@ const useApi = <T>(
): {
isLoading: boolean;
} & Response<T> => {
const [isLoading, setIsLoading] = useState(false);
const [response, setResponse] = useState({
isLoading: true,
success: false,
});
useEffect(() => {
const fetchData = async () => {
setIsLoading(true);
const apiResponse = await fetchApi();
setResponse(apiResponse);
if (!apiResponse.success) {
Toast.show({
type: 'error',
......@@ -25,16 +23,16 @@ const useApi = <T>(
text2: 'Terjadi kesalahan pada sisi kami.',
});
}
setIsLoading(false);
setResponse({
isLoading: false,
...apiResponse,
});
};
fetchData();
}, [fetchApi]);
return {
isLoading,
...response,
};
return response;
};
export default useApi;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment