We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
The idea is instead of writing a component like:
export function MyComponent(props: { doSomethingAsync: () => Promise<'a' | 'b' | 'c'>; }) { const [status, setStatus] = useState<PossibleStates<'a' | 'b' | 'c'>>({ state: 'pending', }); return ( <div> {status.state === 'loading' && 'Loading...'} {status.state === 'error' && 'Error!'} <button onClick={async () => { setStatus({ state: 'loading' }); try { const result = await props.doSomethingAsync(); setStatus({ state: 'success', data: result }); } catch (err) { setStatus({ state: 'error' }); } }} > Click me </button> </div> ); }
We would do this:
export function MyComponent(props: { doSomethingAsync: () => Promise<'a' | 'b' | 'c'>; }) { const [trigger, status] = useAsyncStatus(props.doSomethingAsync); return ( <div> {status.state === 'loading' && 'Loading...'} {status.state === 'error' && 'Error!'} <button onClick={trigger}>Click me</button> </div> ); }
Some considerations
type AsyncFunction<TArgs extends Array<unknown>, TData, TError> = (...args: TArgs) => Promise<{data: TData} | {error: TError}>
export function MyComponent(props: { doSomethingAsync: () => Promise<'a' | 'b' | 'c'>; }) { const [trigger, status, feedbackJsx] = useAsyncStatus(props.doSomethingAsync, { loadingJsx: <span>Loading...</span> errorJsx: <span>Error!</span> }); return ( <div> {feedbackJsx} <button onClick={trigger}>Click me</button> </div> ); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The idea is instead of writing a component like:
We would do this:
Some considerations
eg.
The text was updated successfully, but these errors were encountered: