From ba76f9b580f12dcbf4d227e601cb77274326ea9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Farkas?= Date: Fri, 29 Jul 2022 14:24:33 +0200 Subject: [PATCH] elastic: eliminate react warning (#52934) --- .../QueryEditor/ElasticsearchQueryContext.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx index 77fbf6966c8..8b80c98b84f 100644 --- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx +++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/ElasticsearchQueryContext.tsx @@ -1,4 +1,4 @@ -import React, { Context, createContext, PropsWithChildren, useCallback, useContext } from 'react'; +import React, { Context, createContext, PropsWithChildren, useCallback, useContext, useEffect } from 'react'; import { TimeRange } from '@grafana/data'; @@ -52,11 +52,18 @@ export const ElasticsearchProvider = ({ reducer ); + const isUninitialized = !query.metrics || !query.bucketAggs || query.query === undefined; + // This initializes the query by dispatching an init action to each reducer. // useStatelessReducer will then call `onChange` with the newly generated query - if (!query.metrics || !query.bucketAggs || query.query === undefined) { - dispatch(initQuery()); + useEffect(() => { + if (isUninitialized) { + dispatch(initQuery()); + } + }); + + if (isUninitialized) { return null; }