import * as React from 'react'; export type LoadDataState = { status: 'SUCCESS'; /** Your data. */ result: T; } | { status: 'ERROR'; /** A JavaScript `Error` object. */ error: Error; } | { status: 'LOADING'; }; /** * This function is a react hook for managing the state involved with loading * data. * * ## Example * * ```js * import { useLoadDataState } from '@oneblink/apps-react' * const fetchData = async () => { * const response = await fetch(`https://some-website.com/api?data=data`) * * if (!response.ok) { * const text = await response.text() * throw new Error(text) * } * * return await response.json() * } * * const MyComponent = () => { * const [state, refresh, setResult] = useLoadDataState(fetchData) * * switch (state.status) { * case 'LOADING': * return * case 'ERROR': * return * case 'SUCCESS': * // RENDER UI * } * } * * export default MyComponent * ``` * * @typeParam T The type of the data returned by your `onLoad` function * @param onLoad The function that fetches your data. Should be a Promise that * returns your data * @returns * @group Hooks */ export default function useLoadDataState(onLoad: (abortSignal: AbortSignal) => Promise): [ state: LoadDataState, handleRefresh: () => void, setResult: React.Dispatch> ];