From 2cbd7f1be631d5d985ed5a5961450bef15e43f06 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Mon, 22 May 2023 15:43:22 +0100 Subject: [PATCH] OpenTSDB: use an effect to get aggregators and filters (#68785) * only update state if it's different * use an effect --- .../components/OpenTsdbQueryEditor.tsx | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/components/OpenTsdbQueryEditor.tsx b/public/app/plugins/datasource/opentsdb/components/OpenTsdbQueryEditor.tsx index 53afa9b4dfe..ec2518be510 100644 --- a/public/app/plugins/datasource/opentsdb/components/OpenTsdbQueryEditor.tsx +++ b/public/app/plugins/datasource/opentsdb/components/OpenTsdbQueryEditor.tsx @@ -1,5 +1,5 @@ import { css } from '@emotion/css'; -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { GrafanaTheme2, QueryEditorProps, textUtil } from '@grafana/data'; import { useStyles2 } from '@grafana/ui'; @@ -62,17 +62,21 @@ export function OpenTsdbQueryEditor({ query.downsampleFillPolicy = 'none'; } - datasource.getAggregators().then((aggs: string[]) => { - if (aggs.length !== 0) { - setAggregators(aggs); - } - }); + useEffect(() => { + datasource.getAggregators().then((aggs: string[]) => { + if (aggs.length !== 0) { + setAggregators(aggs); + } + }); + }, [datasource]); - datasource.getFilterTypes().then((filterTypes: string[]) => { - if (filterTypes.length !== 0) { - setFilterTypes(filterTypes); - } - }); + useEffect(() => { + datasource.getFilterTypes().then((newFilterTypes: string[]) => { + if (newFilterTypes.length !== 0) { + setFilterTypes(newFilterTypes); + } + }); + }, [datasource]); async function suggestMetrics(value: string): Promise> { return datasource.metricFindQuery(`metrics(${value})`).then(getTextValues);