React Concurrent Mode is enabled by default for Blitz apps. So the
<Suspense> component is used for loading states and <ErrorBoundary> is
used to display errors. If you need, you can read the
React Concurrent Mode Docs.
import {Suspense} from "react"
import {useQuery, useRouter, useParam} from "blitz"
import getProject from "app/projects/queries/getProject"
import ErrorBoundary from "app/components/ErrorBoundary"
function Project() {
const router = useRouter()
const projectId = useParam("projectId", "number")
const [project] = useQuery(getProject, {where: {id: projectId}})
return <div>{project.name}</div>
}
function WrappedProject() {
return (
<div>
<ErrorBoundary
fallback={(error) => <div>Error: {JSON.stringify(error)}</div>}
>
<Suspense fallback={<div>Loading...</div>}>
<Project />
</Suspense>
</ErrorBoundary>
</div>
)
}
export default WrappedProjectFor more examples and use cases, see the query usage documentation.
const [
queryResult,
{
isFetching,
failureCount,
refetch,
setQueryData,
}
] = useQuery(queryResolver, queryInputArguments, {
enabled,
forceFetchOnMount,
retry,
retryDelay,
staleTime,
cacheTime,
refetchInterval,
refetchIntervalInBackground,
refetchOnWindowFocus,
refetchOnReconnect,
notifiyOnStatusChange,
onSuccess,
onError,
onSettled,
suspense,
initialData,
refetchOnMount,
})queryResolver: A Blitz query resolverqueryInputArgumentsqueryResolveroptionsenabled: Booleanfalse to disable automatic refetching when the query
mounts or changes query keys.refetch method returned from the
useQuery instance.forceFetchOnMount: Booleanfalsetrue to always fetch when the component mounts
(regardless of staleness).retry: Boolean | Int | Function(failureCount, error) => Booleanfalse, failed queries will not retry by default.true, failed queries will retry infinitely.Int, e.g. 3, failed queries will retry until the
failed query count meets that number.(failureCount, error) => boolean failed queries
will retry until the function returns false.retryDelay: Function(retryAttempt: Int) => IntretryAttempt integer and returns the delay
to apply before the next attempt in milliseconds.attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)
applies exponential backoff.attempt => attempt * 1000 applies linear backoff.staleTime: Int | InfinityInfinity, query will never go stalecacheTime: Int | InfinityInfinity, will disable garbage collectionrefetchInterval: false | IntegerrefetchIntervalInBackground: Booleantrue, queries that are set to continuously refetch with a
refetchInterval will continue to refetch while their tab/window is
in the backgroundrefetchOnWindowFocus: Booleanfalse to disable automatic refetching on window focus
(useful, when refetchAllOnWindowFocus is set to true).true to enable automatic refetching on window focus
(useful, when refetchAllOnWindowFocus is set to false.refetchOnReconnect: Booleantrue or false to enable/disable automatic refetching
on reconnect for this query.notifyOnStatusChange: Booleanfalse, the component will only re-render when the actual
data or error changes.true.onSuccess: Function(data) => dataonError: Function(err) => voidonSettled: Function(data, error) => datainitialData: any | Function() => anyrefetchOnMount: Booleantruefalse, will disable additional instances of a query to
trigger background refetchessuspense: Booleanfalse to disable suspense mode.[queryResult, queryExtras]
queryResult: Anyundefined.queryExtras: ObjectisFetching: Booleantrue if the query is currently fetching, including
background fetching.failureCount: Integer0 when the query succeeds.refetch() - Function({ force, throwOnError }) => voidforce: true option and
refetch it regardless of it's freshnessthrowOnError: true optionsetQueryData() - Function(newData, opts) => Promise<void>newData can be an object of new data or a function that receives the
old data and returns the new datarefetch() to
ensure the data is correct. Disable refetch by passing an options
object {refetch: false} as the second argument.setQueryData()