useBackend
Retrieve the analytical backend from BackendProvider context, with an optional prop override or strict variant.
useBackend returns the analytical backend provided to BackendProvider. If you pass a backend, the hook returns it instead, so a component can accept a backend prop that wins over the context. useBackendStrict does the same but throws when no backend is available.
Basic usage
import { useBackend } from "@gooddata/sdk-ui";
function MyComponent({ backend: backendProp }) {
// Uses backendProp if defined, otherwise falls back to context value
const backend = useBackend(backendProp);
// backend may be undefined if neither prop nor context is set
if (!backend) {
return <div>No backend configured</div>;
}
// Use backend to interact with GoodData
// ...
}
Require a backend
useBackendStrict throws an invariant error when neither the parameter nor the context supplies a backend. Use it when a backend is required and you want a clear error instead of handling undefined.
import { useBackendStrict } from "@gooddata/sdk-ui";
function MyComponent({ backend: backendProp }) {
// Throws if no backend is available
const backend = useBackendStrict(backendProp);
// backend is always defined here
const workspaces = backend.workspaces();
// ...
}
Parameters
| Parameter | Type | Description |
|---|---|---|
backend | IAnalyticalBackend | undefined | Optional backend override. If provided, returned directly instead of context value. |
Return value
useBackend:IAnalyticalBackend | undefineduseBackendStrict:IAnalyticalBackend(throws if unavailable)
Notes
- The
backendparameter takes precedence over the context value. The hook returnsbackend ?? backendFromContext. - Without a
BackendProviderup the tree and without thebackendparameter,useBackendreturnsundefined. It doesn’t throw. useBackendStrictaccepts a second, optionalcontext: stringparameter (default"useBackendStrict"). It prefixes the invariant error message, so you can pass the name of the calling hook or component.- When no backend is available,
useBackendStrictraises an invariant error with this message:<context>: IAnalyticalBackend must be defined. Either pass it as a parameter or make sure there is a BackendProvider up the component tree.
Props
| Prop | Type | Required | Description | Inherited from |
|---|---|---|---|---|
authenticate | (force?: boolean) => Promise<IAuthenticatedPrincipal> | Yes | Triggers authentication process against the backend. | — |
capabilities | IBackendCapabilities | Yes | Capabilities available on this backend. | — |
config | IAnalyticalBackendConfig | Yes | Configuration used for communication with this backend. | — |
currentUser | () => IUserService | Yes | Returns a service for interacting with the currently authenticated user. | — |
dataSources | () => IDataSourcesService | Yes | Returns service that can be used to work with data sources. | — |
deauthenticate | (returnTo?: string) => Promise<void> | Yes | Triggers deauthentication process against the backend. | — |
entitlements | () => IEntitlements | Yes | Returns service that can be used to obtain license entitlements. | — |
geo | () => IGeoService | Yes | Returns service that can be used to load geo location assets. | — |
isAuthenticated | () => Promise<IAuthenticatedPrincipal | null> | Yes | Tests authentication against this backend. | — |
onHostname | (hostname: string) => IAnalyticalBackend | Yes | Creates new instance of backend on the provided hostname. | — |
organization | (organizationId: string) => IOrganization | Yes | Returns an organization available on the backend. | — |
organizations | () => IOrganizations | Yes | Returns a service that can be obtained to obtain organizations. | — |
withAuthentication | (provider: IAuthenticationProvider) => IAnalyticalBackend | Yes | Sets authentication provider to be used when backend discovers current session is not authenticated. | — |
withCorrelation | (correlationMetadata: IRequestCorrelationMetadata) => IAnalyticalBackend | Yes | Sets request correlation metadata that will be included in HTTP requests to the backend. If the IAnalyticalBackend instance already has correlation metadata set, the new metadata will be merged with the existing one. This method provides more flexibility than withTelemetry by allowing arbitrary key-value pairs to be sent. | — |
withTelemetry | (componentName: string, props: object) => IAnalyticalBackend | Yes | Sets telemetry information that SHOULD be sent to backend to track component usage. | — |
workspace | (id: string) => IAnalyticalWorkspace | Yes | Returns an analytical workspace available on this backend. | — |
workspaces | () => IWorkspacesQueryFactory | Yes | Returns service that can be used to obtain available workspaces. | — |
Related
Read the workspace identifier from WorkspaceProvider context, with an optional override and a strict variant that throws when unset.
Read the organization identifier supplied to OrganizationProvider, returning the id string or undefined if unset.
Add GoodData.UI to an existing React application by installing packages and styles, configuring backend and workspace contexts, CORS, and authentication.
Pick how your GoodData.UI application authenticates against GoodData Cloud or GoodData.CN — the GoodData session cookie, a JWT from your own portal, or an API token — and set it up.
Configure backend response caching with withCaching, including execution result expiration, cache limits, and the recommended defaults.