Return a portion of a JSON object located at a remote server using HTTP range requests
function Widget({ url }) {
const [ { results }, more ] = usePartialJSON(url, { partial: 'results', chunkSize: 100 * 1024 });
/* ... */
}
url
-<string>
URL of the remote JSON objectoptions
-{ delay, partial }
return
-<array>
or<object>
chunkSize
-<number>
The number of bytes to fetch in one request (Default:5120
)delay
-<number>
Time interval in millisecond between component updates (Default:100
)maxRetries
-<number>
Maximum number of retries after encountering an error that ispartial
-<string>
or<string[]>
The path to the sub-object that can be partially retrieved (Default:""
)retryInterval
-<number>
Number of milliseconds to wait between retries (Default:30000
)
usePartialJSON
returns an empty array initially.
usePartialJSON
will throw if the fetch operation encounters an error (if the server does not
support range requests, for instance). Catch the error using a React error boundary.
If url
is falsy, no attempt to fetch the resource would occur.
The hook relies on React-seq.
SeeConsult documentation of React-seq
for an explanation of how delay
affects the component update frequency.