useCustomWidgetExecutionDataView and useCustomWidgetInsightDataView
Load execution or visualization data inside a custom dashboard widget with the dashboard filters applied automatically.
useCustomWidgetExecutionDataView and useCustomWidgetInsightDataView load data for a custom dashboard widget and apply the filters currently set on the dashboard automatically.
Basic usage
useCustomWidgetExecutionDataView
Run your own execution definition with the dashboard filters applied:
import { type ICustomWidget, useCustomWidgetExecutionDataView } from "@gooddata/sdk-ui-dashboard";
import * as Md from "./catalog.js";
export function MyCustomWidget({ widget }: { widget: ICustomWidget }) {
const { result, status, error } = useCustomWidgetExecutionDataView({
widget,
execution: {
seriesBy: [Md.Revenue],
slicesBy: [Md.Region],
},
});
if (status === "loading" || status === "pending") {
return <div>Loading...</div>;
}
if (status === "error") {
return <div>Error: {error?.message}</div>;
}
const slices = result?.data().slices().toArray() ?? [];
return <div>{slices.length} rows</div>;
}
useCustomWidgetInsightDataView
Load the data of a saved visualization with the dashboard filters merged in:
import { useMemo } from "react";
import { idRef } from "@gooddata/sdk-model";
import { type ICustomWidget, useCustomWidgetInsightDataView } from "@gooddata/sdk-ui-dashboard";
export function MyVisualizationWidget({ widget, visualizationId }: { widget: ICustomWidget; visualizationId: string }) {
const insight = useMemo(() => idRef(visualizationId, "insight"), [visualizationId]);
const { result, status, error } = useCustomWidgetInsightDataView({
widget,
insight,
});
if (status === "loading" || status === "pending") {
return <div>Loading...</div>;
}
if (status === "error") {
return <div>Error: {error?.message}</div>;
}
const slices = result?.data().slices().toArray() ?? [];
return <div>{slices.length} rows</div>;
}
Parameters
useCustomWidgetExecutionDataView
| Property | Type | Description |
|---|---|---|
widget | ICustomWidget | The custom widget. Determines which dashboard filters apply. |
execution | IExecutionConfiguration (without filters) | Execution definition. The dashboard filters are added automatically. |
enableExecutionCancelling | boolean | Cancel in-flight requests when the execution changes. |
useCustomWidgetInsightDataView
| Property | Type | Description |
|---|---|---|
widget | ICustomWidget | The custom widget. Determines which dashboard filters apply. |
insight | IInsightDefinition | ObjRef | The visualization to execute, or a reference to it. When not provided, the hook stays in the "pending" state. |
enableExecutionCancelling | boolean | Cancel in-flight requests when the execution changes. |
Both hooks also take the callbacks onSuccess, onError, onLoading, onPending, and onCancel.
Return value
Both hooks return UseCancelablePromiseState<DataViewFacade, GoodDataSdkError>.
States
| status | result | error | When |
|---|---|---|---|
"pending" | undefined | undefined | execution or insight is not provided, or the widget filters are not resolved yet (for example, right after widget changes). |
"loading" | undefined | undefined | The visualization is being resolved (useCustomWidgetInsightDataView; loaded from the backend when insight is an ObjRef), the widget filter query is running, or the execution is running. |
"success" | DataViewFacade | undefined | The widget filters are resolved and all data of the execution is loaded. In useCustomWidgetInsightDataView, a rejected widget filter query also ends here, with the visualization executed without dashboard filters. |
"error" | undefined | GoodDataSdkError | Loading the visualization, the widget filter query, or the execution failed. In useCustomWidgetExecutionDataView, a rejected filter query also ends in this state with an UnexpectedSdkError (“The widget filter query was rejected”). |
Notes
- Use the hooks inside a dashboard (for example, in a custom widget component). They read the dashboard filters and settings from the dashboard store.
enableExecutionCancellingdefaults to the dashboard settingenableExecutionCancelling, which isfalsewhen not set.- Both hooks take the backend and workspace from
BackendProviderandWorkspaceProvider; there is no prop to pass them. Without a provider, they throw an error such asIAnalyticalBackend must be defined. Either pass it as a parameter or make sure there is a BackendProvider up the component tree., prefixed withuseExecutionDataView:oruseBackendStrict:. - Pass a stable
insighttouseCustomWidgetInsightDataView, for example by wrappingidRef(...)inuseMemo. A newObjRefor visualization definition object on every render counts as a changed input, so the hook resets to"loading"and loads the visualization again on every render. Because each load re-renders your component, the hook never settles in"success". - In
useCustomWidgetInsightDataView,onLoadingandonPendingfire only for the visualization loading phase.onSuccessfires when the data view is loaded. onErroralso fires when the widget filter query fails. InuseCustomWidgetExecutionDataView, it also fires when the filter query is rejected.- The hooks read all data of the execution. There is no paging window.
useCustomWidgetExecutionDataViewpasses the filters available at the moment to the execution, so an execution can start before the widget filters are resolved. The returned state stays"pending"or"loading"until the filter query succeeds and the data view for those filters is loaded.
When they re-run
- The widget filters are re-queried when the widget
ref, itsignoreDashboardFilters, itsdateDataSet, or the dashboard filters (including their selected values) change. useCustomWidgetExecutionDataViewre-executes whenbackend,workspace,enableExecutionCancelling, or the execution fingerprint changes. The fingerprint includes the resolved filters, so a newexecutionobject with the same content does not trigger a new execution.useCustomWidgetInsightDataViewreloads the visualization whenbackend,workspace, orinsightchanges.insightis compared by reference. It re-executes when the visualization or the serialized widget filters change.- When the inputs change before the previous load finishes, the previous result is discarded and
onCancelis called. The underlying execution request is aborted only when execution cancelling is enabled. The same happens on unmount.
Props
| Prop | Type | Required | Description | Inherited from |
|---|---|---|---|---|
widget | ICustomWidget | Yes | Custom widget in the context of which the execution should be run. This affects which filters will be used. | — |
enableExecutionCancelling | boolean | No | Enable or disable real execution cancellation. This means that if the execution request is not yet finished and the execution changes, the request will be cancelled and the new execution will be started. | — |
execution | Exclude<IExecutionConfiguration, "filters"> | No | Definition of the execution to execute (without filters). The filters will be filled automatically. | — |
Related
Choose which layer of a dashboard widget to replace (the widget, the visualization, or its body) and how drills flow through custom renderers.
Resolve the effective filters of a dashboard widget and visualization, ready to apply with insightSetFilters before execution.
Fetch execution data as a DataViewFacade from a prepared execution or inline seriesBy/slicesBy configuration.
Load a saved visualization by reference and return its execution result as a DataViewFacade with optional filters and sorting.
Implement the dashboard plugin lifecycle, customize visualizations, widgets, layouts, filters, and exports, and register dashboard event handlers.