useDashboardSelector
Read Dashboard component state with useDashboardSelector and the select* selectors, from components, event handlers, and outside code.
useDashboardSelector reads one value from the state of the Dashboard component. Pass it a selector; it returns the selector’s result and re-renders your component when that result changes.
Selectors are memoized: for the same state they return the same reference, so their results are safe to use as dependencies of useMemo and useEffect.
The component that calls the hook must render inside the Dashboard component. That covers custom widgets, components registered through the customization API, and plugin decorators. For code that runs outside the component, see Read state outside the Dashboard component.
Basic usage
The most common use is to hand rendering back to the built-in component in edit mode:
import {
DefaultDashboardInsight,
selectIsInEditMode,
useDashboardSelector,
type IDashboardInsightProps,
} from "@gooddata/sdk-ui-dashboard";
export function CustomInsight(props: IDashboardInsightProps) {
const isInEditMode = useDashboardSelector(selectIsInEditMode);
return isInEditMode ? (
<DefaultDashboardInsight {...props} />
) : (
<div>Custom rendering of {props.widget.title}</div>
);
}
Register the component with customize.insightWidgets().withCustomProvider(...) in your plugin. See Dashboard plugins API.
Selectors with parameters
Some selectors are factories. Call the factory with the parameter and pass the result to the hook. Factories are memoized per argument, so calling one during render doesn’t create a new selector each time.
import { attributeDisplayFormRef } from "@gooddata/sdk-model";
import { selectFilterContextAttributeFilterItemByDisplayForm, useDashboardSelector } from "@gooddata/sdk-ui-dashboard";
import * as Md from "./catalog.js";
const regionDisplayForm = attributeDisplayFormRef(Md.Region.Default);
export function RegionFilterSummary() {
const regionFilter = useDashboardSelector(
selectFilterContextAttributeFilterItemByDisplayForm(regionDisplayForm),
);
return <span>{regionFilter ? "Region filter is set" : "No region filter"}</span>;
}
Turn a customization on per dashboard
Dashboard tags let a plugin decide, per dashboard, whether to apply itself. Dashboard authors add or remove the tag in GoodData Cloud, so you don’t need to redeploy the plugin. Use the pattern from Basic usage, with a tag check in place of the edit-mode check:
const isEnabled = useDashboardSelector(selectDashboardTags).includes("custom-rendering");
To decide by the visualization’s tag instead of the dashboard’s, use customize.insightWidgets().withTag(tag, component).
Parameters
| Parameter | Type | Description |
|---|---|---|
selector | (state: DashboardState) => TResult | The selector to evaluate, usually one of the select* functions exported by @gooddata/sdk-ui-dashboard. |
Commonly used selectors
| Selector | Returns | Use it to |
|---|---|---|
selectIsInEditMode | boolean | Fall back to the default component in edit mode |
selectIsExport | boolean | Simplify rendering when the dashboard is exported to PDF |
selectDashboardId | string | undefined | Key persisted UI state or build links to the dashboard |
selectDashboardRef | ObjRef | undefined | Pass the dashboard to IAnalyticalBackend calls |
selectDashboardTitle | string | Show the title in a custom top bar |
selectDashboardTags | string[] | Turn a customization on for selected dashboards |
selectFilterContextFilters | FilterContextItem[] | Read every dashboard-level filter |
selectFilterContextDateFilter | IDashboardDateFilter | undefined | Read the dashboard date filter |
selectFilterContextAttributeFilterItemByDisplayForm(ref) | DashboardAttributeFilterItem | undefined | Read one attribute filter by its display form |
selectLayout | IDashboardLayout | Walk sections, items, and widgets |
selectInsights | IInsight[] | List the visualizations placed on the dashboard |
selectSettings | ISettings | Read workspace and user settings |
selectLocale | ILocale | Format text for the dashboard locale |
The full list is in the API reference.
Return value
The selector’s result for the current dashboard state. The component re-renders when the result changes.
Notes
- Selectors marked
@alphaor@internalon their reference page can change or disappear in a minor release. See API maturity. - To get the effective
IFilter[]for a widget, useuseWidgetFiltersinstead of mergingselectFilterContextFilterswith the visualization’s own filters yourself. The hook applies the widget’s ignored filters and date dataset for you. - Selectors only read. To change the dashboard, dispatch a command with
useDispatchDashboardCommand; see Dashboard component. useDashboardSelectorthrows when rendered outside theDashboardcomponent tree. Code outside the tree reads state throughSingleDashboardStoreAccessor; see Read state outside the Dashboard component.
Read state in an event handler
Event handlers receive a stateSelect function that accepts the same selectors as the hook. See Handle dashboard events.
Read state outside the Dashboard component
Pass the change handler of SingleDashboardStoreAccessor to the onStateChange prop. The accessor then exposes a DashboardSelectorEvaluator you can call from anywhere in your application.
import { Dashboard, selectDashboardTitle, SingleDashboardStoreAccessor } from "@gooddata/sdk-ui-dashboard";
export function DashboardPage({ dashboard }: { dashboard: string }) {
return <Dashboard dashboard={dashboard} onStateChange={SingleDashboardStoreAccessor.getOnChangeHandler()} />;
}
export function currentDashboardTitle(): string | undefined {
if (!SingleDashboardStoreAccessor.isAccessorInitialized()) {
return undefined;
}
return SingleDashboardStoreAccessor.getDashboardSelect()(selectDashboardTitle);
}
Call getDashboardSelect() each time you need a value. The component replaces the evaluator after every state change, so a stored one keeps returning stale state.
To follow more than one dashboard, use DashboardStoreAccessorRepository. See Access the state of the Dashboard component from outside the component.
Related
Embed a saved dashboard with Dashboard, read its state through selectors, dispatch commands, handle events, configure behavior, and access its store externally.
Register typed handlers for Dashboard component events, read state and dispatch commands from a handler, and match commands to events.
Get the dispatch function of the dashboard store with the alpha useDashboardDispatch hook, or use the public useDispatchDashboardCommand.
Resolve the effective filters of a dashboard widget and visualization, ready to apply with insightSetFilters before execution.
Choose which layer of a dashboard widget to replace (the widget, the visualization, or its body) and how drills flow through custom renderers.