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 WrappedProject
For 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 resolverqueryInputArguments
queryResolver
options
enabled: Boolean
false
to disable automatic refetching when the query
mounts or changes query keys.refetch
method returned from the
useQuery
instance.forceFetchOnMount: Boolean
false
true
to always fetch when the component mounts
(regardless of staleness).retry: Boolean | Int | Function(failureCount, error) => Boolean
false
, 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) => Int
retryAttempt
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 | Infinity
Infinity
, query will never go stalecacheTime: Int | Infinity
Infinity
, will disable garbage collectionrefetchInterval: false | Integer
refetchIntervalInBackground: Boolean
true
, queries that are set to continuously refetch with a
refetchInterval
will continue to refetch while their tab/window is
in the backgroundrefetchOnWindowFocus: Boolean
false
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: Boolean
true
or false
to enable/disable automatic refetching
on reconnect for this query.notifyOnStatusChange: Boolean
false
, the component will only re-render when the actual
data
or error
changes.true
.onSuccess: Function(data) => data
onError: Function(err) => void
onSettled: Function(data, error) => data
initialData: any | Function() => any
refetchOnMount: Boolean
true
false
, will disable additional instances of a query to
trigger background refetchessuspense: Boolean
false
to disable suspense mode.[queryResult, queryExtras]
queryResult: Any
undefined
.queryExtras: Object
isFetching: Boolean
true
if the query is currently fetching, including
background fetching.failureCount: Integer
0
when the query succeeds.refetch()
- Function({ force, throwOnError }) => void
force: 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()