useWorkspace
Read the workspace identifier from WorkspaceProvider context, with an optional override and a strict variant that throws when unset.
useWorkspace returns the workspace identifier provided to WorkspaceProvider. If you pass a workspace identifier, the hook returns it instead, so a component can accept a workspace prop that wins over the context. useWorkspaceStrict does the same but throws when no workspace is available.
Basic usage
import { useWorkspace } from "@gooddata/sdk-ui";
function MyComponent({ workspace: workspaceProp }: { workspace?: string }) {
// Uses workspaceProp if defined, otherwise falls back to the context value
const workspace = useWorkspace(workspaceProp);
if (!workspace) {
return <div>No workspace configured</div>;
}
// Use the workspace identifier for data fetching
// ...
}
Require a workspace
Use useWorkspaceStrict in components that can’t work without a workspace. It throws an invariant error when neither the parameter nor the context supplies one, so a misconfigured provider tree fails early:
import { useWorkspaceStrict } from "@gooddata/sdk-ui";
function MyComponent({ workspace: workspaceProp }: { workspace?: string }) {
// Throws if no workspace is available
const workspace = useWorkspaceStrict(workspaceProp);
// workspace is always a string here
// ...
}
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace | string | undefined | Optional workspace override. If defined, the hook returns it instead of the context value. |
context | string | useWorkspaceStrict only. Optional prefix of the error message, "useWorkspaceStrict" by default. Pass the name of the calling hook or component. |
Return value
useWorkspace:string | undefineduseWorkspaceStrict:string(throws if unavailable)
Notes
- The override wins only when it’s defined. Passing
undefinedis the same as passing nothing. - Components re-render when
WorkspaceProviderreceives a newworkspacevalue, so hooks that key cached data on the workspace pick up the change. - When no workspace is available,
useWorkspaceStrictthrows with this message:<context>: workspace must be defined. Either pass it as a parameter or make sure there is a WorkspaceProvider up the component tree.
Related
Retrieve the analytical backend from BackendProvider context, with an optional prop override or strict variant.
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.
Load measures, attributes, and date datasets while your application runs, and turn catalog items into IMeasure and IAttribute objects.