Files
grafana/public/app/features/explore/Graph/useStructureRev.ts
Giordano Ricci 6b6b733229 Explore: Fix graph not updating when changing config (#62473)
* Explore: Fix graph not updating when changing config

* move useStructureRev to a seprate hook and add specific tests
2023-02-01 16:23:02 +00:00

21 lines
760 B
TypeScript

import { useMemo } from 'react';
import { useCounter, usePrevious } from 'react-use';
import { DataFrame, compareArrayValues, compareDataFrameStructures } from '@grafana/data';
export function useStructureRev(frames: DataFrame[]) {
const [structureRev, { inc }] = useCounter(0);
const previousFrames = usePrevious(frames);
// We need to increment structureRev when the number of series changes.
// the function passed to useMemo runs during rendering, so when we get a different
// amount of data, structureRev is incremented before we render it
useMemo(() => {
if (previousFrames && !compareArrayValues(frames, previousFrames, compareDataFrameStructures)) {
inc();
}
}, [frames, previousFrames, inc]);
return structureRev;
}