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>( ...@@ -8,16 +8,14 @@ const useApi = <T>(
): { ): {
isLoading: boolean; isLoading: boolean;
} & Response<T> => { } & Response<T> => {
const [isLoading, setIsLoading] = useState(false);
const [response, setResponse] = useState({ const [response, setResponse] = useState({
isLoading: true,
success: false, success: false,
}); });
useEffect(() => { useEffect(() => {
const fetchData = async () => { const fetchData = async () => {
setIsLoading(true);
const apiResponse = await fetchApi(); const apiResponse = await fetchApi();
setResponse(apiResponse);
if (!apiResponse.success) { if (!apiResponse.success) {
Toast.show({ Toast.show({
type: 'error', type: 'error',
...@@ -25,16 +23,16 @@ const useApi = <T>( ...@@ -25,16 +23,16 @@ const useApi = <T>(
text2: 'Terjadi kesalahan pada sisi kami.', text2: 'Terjadi kesalahan pada sisi kami.',
}); });
} }
setIsLoading(false); setResponse({
isLoading: false,
...apiResponse,
});
}; };
fetchData(); fetchData();
}, [fetchApi]); }, [fetchApi]);
return { return response;
isLoading,
...response,
};
}; };
export default useApi; 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