0
Fork 0
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:
simeng-li 2021-12-21 13:28:14 +08:00 committed by GitHub
parent dd7a385363
commit 3a80d99c9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,7 +16,8 @@ function useApi<Args extends any[], Response>(
const [error, setError] = useState<RequestErrorBody | null>(null);
const [result, setResult] = useState<Response>();
const run = async (...args: Args) => {
const run = useCallback(
async (...args: Args) => {
setLoading(true);
setError(null);
@ -35,7 +36,9 @@ function useApi<Args extends any[], Response>(
setLoading(false);
throw error;
}
};
},
[api]
);
return {
loading,