Skip to main content

useContent

Searches project content for a content type and language.

When to use it​

Both type and language must be non-empty; an empty value keeps the request disabled.

Setup​

Call it from a client-side React component below OvokProvider so it uses the client configured at that provider boundary.

Hook arguments​

ArgumentTypeDetails
typestringContent type; an empty value disables the request.
languagestringLanguage to search; an empty value disables the request.
params (optional)OvokContentQueryOptional content search filters.

TypeScript signature​

(type: string, language: string, params?: OvokContentQuery): OvokRequestState<OvokContentSearchResponse>

Request state​

The response payload is OvokContentSearchResponse. A successful empty result, a failed request, and a request that has not returned data are distinct states.

FieldMeaning
dataLatest successful response, or undefined before one is available.
loadingWhether the request is in progress.
errorRequest error as an Error, or undefined when no request has failed.
reloadRepeats the request with the current arguments.

Request behavior​

The hook loads when its required inputs are available and reloads when those inputs change. Use reload for an explicit refresh; render loading, error, and successful no-result states separately.

Example​

Handle the hook's loading and error states before rendering the result.

import { useContent } from '@ovok/core';

export function ContentExample({ type, language, params }: { type: Parameters<typeof useContent>[0]; language: Parameters<typeof useContent>[1]; params?: Parameters<typeof useContent>[2] }) {
const { data, loading, error, reload } = useContent(type, language, params);
if (loading) return <p>Loading…</p>;
if (error) return <p role="alert">{error.message}</p>;
return <section><button onClick={reload}>Reload</button><pre>{JSON.stringify(data, null, 2)}</pre></section>;
}