0.1.19 • Published 10 months ago
@quilted/preact-async v0.1.19
@quilted/preact-async
import {useAsync} from '@quilted/preact-async';
function MyComponent() {
const result = useAsync(
async () => {
const response = await fetch('https://api.example.com/data');
return response.json();
},
{
server: true,
},
);
if (result.pending) {
return <LoadingSpinner />;
}
if (result.error) {
return <ErrorMessage error={result.error} />;
}
return <DataDisplay data={result.value} />;
}
import {useAsyncModule, AsyncModule} from '@quilted/preact-async';
const asyncModule = new AsyncModule(() => import('./my-module.ts'));
console.log(asyncModule.status);
console.log(asyncModule.imported);
console.log(asyncModule.reason);
console.log(asyncModule.url.href);
const loaded = await asyncModule.import();
const loaded2 = await asyncModule.promise;
function MyComponent() {
const {moduleMethod} = useAsyncModule(asyncModule);
return <DataDisplay data={moduleMethod()} />;
}
function MyComponent2() {
if (asyncModule.status !== 'resolved') {
return <button onClick={() => asyncModule.import()}>Load it!</button>;
}
const {moduleMethod} = asyncModule.imported!;
return <DataDisplay data={moduleMethod()} />;
}
import {AsyncModule, AsyncComponent} from '@quilted/preact-async';
const module = new AsyncModule(() => import('./MyComponent.tsx'));
<AsyncComponent
module={module}
render={({default: Component}) => <Component />}
/>;
const MyComponent = AsyncComponent.from(module, {
renderLoading: <LoadingSpinner />,
});
<MyComponent />;
react-query
APIs to consider
@see https://tanstack.com/query/latest/docs/framework/react/reference/useQuery
Options
queryKey
(askey
)queryFn
(asfunction
)enabled
(asactive
)networkMode
(no — implement in userland if needed)- -
retry
(withuseAsyncRetry()
) retryOnMount
(no — useuseAsyncRetry
or a manualuseEffect
instead)staleTime
gcTime
queryKeyHashFn
(no — convertkey
to a string ahead of time if you want this)refetchInterval
refetchIntervalInBackground
refetchOnMount
refetchOnWindowFocus
refetchOnReconnect
notifyOnChangeProps
(no — uses signals for all mutable properties)select
(no — create a computed signal instead)initialData
(ascached.value
)initialDataUpdatedAt
(ascached.time
)placeholderData
(no — do this in your component instead)structuralSharing
(no — out of scope)throwOnError
(no — do this in your component instead)meta
(no — manually write this to the returnedAsyncAction
instance, since it is directly cached)queryClient
(ascache
)
Returns
status
(different values, though)isPending
(asisRunning
)isSuccess
(no — usestatus === 'resolved'
orvalue
instead)isError
(no — usestatus === 'rejected'
orerror
instead)isLoadingError
(no — seeisError
)isRefetchError
(no — implement in userland if needed)data
(aliased asvalue
)dataUpdatedAt
(TODO asresolved.updatedAt
)error
errorUpdatedAt
(asupdatedAt
, but only if error was the last result)failureCount
(no — implement in userland if needed)failureReason
(no — implement in userland if needed)isStale
isFetched
(ashasFinished
)isFetchedAfterMount
(no — implement in userland if needed)fetchStatus
(no — implement in userland if needed)isPaused
(no — implement in userland if needed)isRefetching
(no — useisRunning && hasFinished
instead)isLoading
(no — useisRunning && !hasFinished
instead)isInitialLoading
(no — useisRunning && !hasFinished
instead)errorUpdateCount
(no — implement in userland if needed)refetch
(asrerun
)
0.0.0-preview-20240904052234
10 months ago
0.1.19
10 months ago
0.0.0-preview-20240830040003
10 months ago
0.1.18
10 months ago
0.1.10
1 year ago
0.0.0-preview-20240609003831
1 year ago
0.1.11
1 year ago
0.1.12
1 year ago
0.1.13
1 year ago
0.1.14
1 year ago
0.1.15
11 months ago
0.0.0-preview-20240529043830
1 year ago
0.0.0-preview-20240522043441
1 year ago
0.0.0-preview-20240526144411
1 year ago
0.0.0-preview-20240522051410
1 year ago
0.0.0-preview-20240523032146
1 year ago
0.0.0-preview-20240526000508
1 year ago
0.0.0-preview-20240529051332
1 year ago
0.0.0-preview-20240727054458
11 months ago
0.0.0-preview-20240530025520
1 year ago
0.0.0-preview-20240522053303
1 year ago
0.1.8
1 year ago
0.1.7
1 year ago
0.1.9
1 year ago
0.0.0-preview-20240526000644
1 year ago
0.0.0-preview-20240727024619
11 months ago
0.0.0-preview-20240522054106
1 year ago
0.0.0-preview-20240727025347
11 months ago
0.0.0-preview-20240606070638
1 year ago
0.0.0-preview-20240530043627
1 year ago
0.0.0-preview-20240727072500
11 months ago
0.0.0-preview-20240601212754
1 year ago
0.0.0-preview-20240526143218
1 year ago
0.0.0-preview-20240522071038
1 year ago
0.0.0-preview-20240526144941
1 year ago
0.1.16
11 months ago
0.0.0-preview-20240529060857
1 year ago
0.0.0-preview-20240521235738
1 year ago
0.1.17
11 months ago
0.0.0-preview-20240529062719
1 year ago
0.0.0-preview-20240521020748
1 year ago
0.0.0-preview-20240521054222
1 year ago
0.0.0-preview-20240521053520
1 year ago
0.0.0-preview-20240521053157
1 year ago
0.0.0-preview-20240521020221
1 year ago
0.0.0-preview-20240521030838
1 year ago
0.0.0-preview-20240521001127
1 year ago
0.0.0-preview-20240521032737
1 year ago
0.0.0-preview-20240521055634
1 year ago
0.1.4
1 year ago
0.1.3
1 year ago
0.1.6
1 year ago
0.1.5
1 year ago
0.1.2
1 year ago
0.0.0-preview-20240519201920
1 year ago
0.0.0-preview-20240519195620
1 year ago
0.0.0-preview-20240519195503
1 year ago
0.1.1
1 year ago
0.0.0-preview-20240511041214
1 year ago
0.0.0-preview-20240511033022
1 year ago
0.1.0
1 year ago