mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
fix(ui): fix useApi request in useEffect dead loop bug (#158)
wrap the request method in userApi hook into a useCallback
This commit is contained in:
parent
dd7a385363
commit
3a80d99c9f
1 changed files with 21 additions and 18 deletions
|
@ -1,6 +1,6 @@
|
|||
import { RequestErrorBody } from '@logto/schemas';
|
||||
import { HTTPError } from 'ky';
|
||||
import { useState } from 'react';
|
||||
import { useState, useCallback } from 'react';
|
||||
|
||||
type UseApi<T extends any[], U> = {
|
||||
result?: U;
|
||||
|
@ -16,26 +16,29 @@ function useApi<Args extends any[], Response>(
|
|||
const [error, setError] = useState<RequestErrorBody | null>(null);
|
||||
const [result, setResult] = useState<Response>();
|
||||
|
||||
const run = async (...args: Args) => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const run = useCallback(
|
||||
async (...args: Args) => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const result = await api(...args);
|
||||
setResult(result);
|
||||
setLoading(false);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof HTTPError) {
|
||||
const kyError = await error.response.json<RequestErrorBody>();
|
||||
setError(kyError);
|
||||
try {
|
||||
const result = await api(...args);
|
||||
setResult(result);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof HTTPError) {
|
||||
const kyError = await error.response.json<RequestErrorBody>();
|
||||
setError(kyError);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
setLoading(false);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[api]
|
||||
);
|
||||
|
||||
return {
|
||||
loading,
|
||||
|
|
Loading…
Add table
Reference in a new issue