-
Notifications
You must be signed in to change notification settings - Fork 371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ui-core]: feat: add useLoadData hook for API calls #3819
Conversation
const response = await get<T, U>(fetchUrl, localOptions?.params, fetchOptions); | ||
setData(response); | ||
} catch (error) { | ||
setError(error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure that we're setting an Error object. As the type set is Error
setError(error instanceof Error ? error : new Error(error);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work, clean and well written code! Just a couple of questions.
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { ApiFetchOptions, get } from '../../api/utils'; | ||
|
||
export type IOptions<T, U> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of the I-prefix? If it is to indicate an interface, perhaps it would better to use an interface instead of type.
const [loading, setLoading] = useState<boolean>(false); | ||
const [error, setError] = useState<Error | undefined>(); | ||
|
||
const fetchOptionsDefault: ApiFetchOptions<T> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this could be defined outside the component
reloadData: () => void; | ||
}; | ||
|
||
const useLoadData = <T, U = unknown>(url?: string, options?: IOptions<T, U>): IUseLoadData<T> => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the use case for not passing in a url?
* [ui-core]: feat: add useFetch hook for API calls * add the license information * fix lint * lint fix * add library * pin the library version * revert extra changes * revert package-lock json * fix: rename hook * fix the function name * typecaste error object
What changes were proposed in this pull request?
useLoadData
react hook which exposes thedata
,loading
,error
, andrefetch
to the react component.How was this patch tested?
Please review Hue Contributing Guide before opening a pull request.