> Documentation index: https://www.gooddata.ai/docs/gooddata-ui/llms.txt lists every page of these docs as Markdown.

# useWorkspace

> Read the workspace identifier from WorkspaceProvider context, with an optional override and a strict variant that throws when unset.

API reference: [useWorkspace()](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-ui/sdk-ui.useworkspace/index.md) (function), [useWorkspaceStrict()](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-ui/sdk-ui.useworkspacestrict/index.md) (function)

`useWorkspace` returns the workspace identifier provided to [`WorkspaceProvider`](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-ui/sdk-ui.workspaceprovider/index.md). 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](https://www.gooddata.ai/docs/gooddata-ui/latest/integrations/hooks/use-workspace/#basic-usage)

```tsx
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](https://www.gooddata.ai/docs/gooddata-ui/latest/integrations/hooks/use-workspace/#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:

```tsx
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](https://www.gooddata.ai/docs/gooddata-ui/latest/integrations/hooks/use-workspace/#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](https://www.gooddata.ai/docs/gooddata-ui/latest/integrations/hooks/use-workspace/#return-value)

- `useWorkspace`: `string | undefined`
- `useWorkspaceStrict`: `string` (throws if unavailable)

## [Notes](https://www.gooddata.ai/docs/gooddata-ui/latest/integrations/hooks/use-workspace/#notes)

- The override wins only when it's defined. Passing `undefined` is the same as passing nothing.
- Components re-render when [`WorkspaceProvider`](https://www.gooddata.ai/docs/gooddata-ui/latest/api-reference/sdk-ui/sdk-ui.workspaceprovider/index.md) receives a new `workspace` value, so hooks that key cached data on the workspace pick up the change.
- When no workspace is available, `useWorkspaceStrict` throws 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.`
