@dynatrace-sdk/react-hooks

中文文档
1.15.3发布于 2026/10/15Apache-2.0TypeScriptESM + CJS近 30 天下载 1.6万

The @dynatrace-sdk/react-hooks library provides a seamless way to interact with Dynatrace platform service clients in React applications. This package leverages hooks to greatly simplify the use of Dynatrace services within React. For detailed informati

原始使用文档
文档目录6 节
  1. Simplified data querying from Grail
  2. Fetching Grail fields
  3. Interacting with documents and app states
  4. Interacting with Settings
  5. Simplified Use of Davis® Analyzers
  6. App functions

@dynatrace-sdk/react-hooks

The @dynatrace-sdk/react-hooks library provides a seamless way to interact with Dynatrace platform service clients in React applications. This package leverages hooks to greatly simplify the use of Dynatrace services within React. For detailed information, visit our Developer Portal.

npm install @dynatrace-sdk/react-hooks

Simplified data querying from Grail

Traditionally, querying data in Dynatrace involves using the client-query package and managing complex React state. The useDql hook in this package streamlines this process. The following example showcases how to fetch data with a DQL query:

const { data, error, isLoading } = useDql('fetch logs');

This hook is fully compatible with the parameters used in the queryExecute function of the @dynatrace-sdk/client-query package.

For instance, to limit the number of results returned:

const { data, error, isLoading, refetch } = useDql({
  query: 'fetch logs',
  maxResultRecords: 2000,
});

You can delay the execution of the query until a user clicks on a button by passing additional query options to the hook:

const { data, error, isLoading, refetch } = useDql('fetch logs', { enabled: false });

function onClickQuery() {
  refetch();
}

You should add appropriate scopes to your app's configuration based on the query type. For more details, refer to the Bucket and table permissions in Grail documentation.

Fetching Grail fields

The useGrailFields hook fetches Grail fields from both autocomplete and DSS query sources and exposes them separately as autocompleteData and dssData. The dataObject accepts "logs", "spans", "smartscape.nodes", or "metrics".

const { data, isLoading, isError, errors } = useGrailFields({
  autocompleteQueryOptions: { query: 'fetch logs' },
  dssQueryOptions: { dataObject: 'logs' },
});

Use select to transform the raw data before it's returned as data:

const { data } = useGrailFields({
  autocompleteQueryOptions: { query: 'fetch logs' },
  dssQueryOptions: { dataObject: 'logs' },
  select: ({ autocompleteData }) =>
    autocompleteData?.suggestions?.map((s) => s.suggestion ?? '') ?? [],
});

Interacting with documents and app states

Beyond DQL queries, our hooks facilitate interactions with documents and app state. They allow control over immediate or deferred query execution.

const { data, error, isLoading } = useDocument({ id: documentId }, { autoFetch: true });

For creating, updating, or deleting documents or app state, an explicit execute call is necessary:

const { data, execute, error } = useCreateDocument();

function onClickCreateDocument() {
  execute(DOCUMENT_DATA);
}

Depending on your interaction type, add these scopes to your app configuration:

FunctionScope
Document readdocument:documents:read
Document write/updatedocument:documents:write
Document deletedocument:documents:delete
State readstate:app-states:read
State writestate:app-states:write
State deletestate:app-states:delete
User state readstate:user-app-states:read
User state writestate:user-app-states:write
User state deletestate:user-app-states:delete

Interacting with Settings

Settings can be utilized in the following way:

const { data, error, isLoading } = useSettingsV2(
  { schemaIds: SETTING_SCHEMA },
  { autoFetch: true },
);

For creating, updating, or deleting settings, an explicit execute call is necessary:

const { data, execute, error } = useCreateSettingsV2();

function onClickCreateSettings() {
  execute(SETTING_DATA);
}

Depending on your interaction type, add these scopes to your app configuration:

FunctionScope
Settings readapp-settings:objects:read
Settings write/updateapp-settings:objects:write
Settings deleteapp-settings:objects:delete

Simplified Use of Davis® Analyzers

Leveraging Davis® analyzers traditionally involves complex state management and polling logic, alongside the @dynatrace-sdk/client-davis-analyzers package. The useAnalyzer hook in this package makes this process much more straightforward:

const { data, error, isLoading } = useAnalyzer({
  analyzerName: 'dt.statistics.GenericForecastAnalyzer',
  body: {
    timeSeriesData: {
      expression: query,
    },
  },
});

This hook supports all the parameters available in the executeAnalyzer method from the @dynatrace-sdk/client-davis-analyzers package.

To defer the execution of the analyzer until a user action, like a button click, configure the hook with additional options:

const { data, error, isLoading, refetch } = useAnalyzer({
  analyzerName: 'dt.statistics.GenericForecastAnalyzer',
  body: {
    timeSeriesData: {
      expression: query,
    },
  },
  {
    autoFetch: false,
    autoFetchOnUpdate: true,
  }
});

function onExecuteAnalyzer() {
  refetch();
}

In your app's configuration, include the necessary scope:

FunctionScope
Use analyzerdavis:analyzers:execute

App functions

The useAppFunction hook is the simplest way to call app functions. As the other hooks in this package, it provides state handling for loading and error states:

const { data, error, isLoading } = useAppFunction({ name: 'functionName', data: 'data' });

Sometimes you want to delay the execution of the app function until a user clicks on a button. This can be achieved by passing additional options to the hook:

const { data, error, isLoading, refetch } = useAppFunction(
  { name: 'functionName', data: 'data' },
  { autoFetch: false, autoFetchOnUpdate: false },
);

function onClick() {
  refetch();
}

版本与发布记录

@dynatrace-sdk/react-hooks 共发布 37 个版本,首次发布于 2023/08/11;过去 12 个月发布了 14 个版本。

版本发布日期距今
1.15.3latest2026/10/1529 天前
1.15.2tmp-1.15.22026/10/1430 天前
1.15.1tmp-1.15.12026/10/051 个月前
1.15.02026/10/011 个月前
1.14.22026/08/033 个月前
1.14.12026/06/264 个月前

关于 @dynatrace-sdk/react-hooks 的常见问题

@dynatrace-sdk/react-hooks 是什么?

The @dynatrace-sdk/react-hooks library provides a seamless way to interact with Dynatrace platform service clients in React applications. This package leverages hooks to greatly simplify the use of Dynatrace services within React. For detailed informati

如何安装 @dynatrace-sdk/react-hooks?

在项目目录运行 npm install @dynatrace-sdk/react-hooks,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^17.0.0 || ^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dynatrace-sdk/react-hooks 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dynatrace-sdk/react-hooks 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dynatrace-sdk/react-hooks 可以商用吗?

@dynatrace-sdk/react-hooks 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dynatrace-sdk/react-hooks 的下载量有多少?

根据 npm 官方下载统计,@dynatrace-sdk/react-hooks 在 2026/10/13 – 2026/11/11 这 30 天内被下载 16,375 次,日均约 546 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dynatrace-sdk/react-hooks 还在维护吗?

最新版本 1.15.3 发布于 2026/10/15(29 天前);至今共发布 37 个版本,其中过去 12 个月发布了 14 个;首次发布于 2023/08/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。