Skip to content

Commit

Permalink
measurements: Revert back to useMemo
Browse files Browse the repository at this point in the history
Resolves lint warnings from #1837.

I wonder if I ran into lint warnings the first time I tried `useCallback`
which is why I ended up using `useMemo`? Noting in comments so I don't
flip-flop again.
  • Loading branch information
joverlee521 committed Aug 28, 2024
1 parent c585722 commit 344cd28
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/components/measurements/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useRef, useEffect, useState } from "react";
import React, { useCallback, useRef, useEffect, useMemo, useState } from "react";
import { useSelector } from "react-redux";
import { isEqual, orderBy } from "lodash";
import { NODE_VISIBLE } from "../../util/globals";
Expand Down Expand Up @@ -41,17 +41,6 @@ const useDeepCompareMemo = (value) => {
return ref.current;
};

/**
* A wrapper around React's useCallback hook that does a deep comparison of the
* dependencies.
* @param {function} fn
* @param {Array} dependencies
* @returns
*/
const useDeepCompareCallback = (fn, dependencies) => {
return useCallback(fn, dependencies.map(useDeepCompareMemo))
}

// Checks visibility against global NODE_VISIBLE
const isVisible = (visibility) => visibility === NODE_VISIBLE;

Expand Down Expand Up @@ -170,9 +159,11 @@ const MeasurementsPlot = ({height, width, showLegend, setPanelTitle}) => {
}
const groupedMeasurements = groupMeasurements(filteredMeasurements, groupBy, groupByValueOrder);

// Cache D3 scale functions to allow deep comparison to work below for svgData
const xScale = useDeepCompareCallback(createXScale(width, filteredMeasurements), [width, filteredMeasurements]);
const yScale = useCallback(createYScale(), []);
// Memoize D3 scale functions to allow deep comparison to work below for svgData
// Using `useMemo` instead of `useCallback` because `useCallback` is specifically designed for inline functions
// and will raise lint errors, see https://github.com/facebook/react/issues/19240#issuecomment-652945246
const xScale = useMemo(() => createXScale(width, filteredMeasurements), [width, filteredMeasurements].map(useDeepCompareMemo));
const yScale = useMemo(() => createYScale(), []);
// Memoize all data needed for basic SVG to avoid extra re-drawings
const svgData = useDeepCompareMemo({
containerHeight: height,
Expand Down

0 comments on commit 344cd28

Please sign in to comment.