diff --git a/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx b/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx index ef32da8a1cc..6e09ce1a990 100644 --- a/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx +++ b/packages/grafana-ui/src/components/Segment/SegmentAsync.tsx @@ -1,9 +1,11 @@ -import React, { useState } from 'react'; +import React from 'react'; import { cx } from 'emotion'; import _ from 'lodash'; import { SegmentSelect } from './SegmentSelect'; import { SelectableValue } from '@grafana/data'; import { useExpandableLabel, SegmentProps } from '.'; +import { useAsyncFn } from 'react-use'; +import { AsyncState } from 'react-use/lib/useAsync'; export interface SegmentAsyncProps extends SegmentProps { value?: T | SelectableValue; @@ -20,20 +22,14 @@ export function SegmentAsync({ allowCustomValue, placeholder, }: React.PropsWithChildren>) { - const [selectPlaceholder, setSelectPlaceholder] = useState(''); - const [loadedOptions, setLoadedOptions] = useState>>([]); + const [state, fetchOptions] = useAsyncFn(loadOptions, [loadOptions]); const [Label, width, expanded, setExpanded] = useExpandableLabel(false); if (!expanded) { const label = _.isObject(value) ? value.label : value; return (