UNPKG

recharts

Version:
56 lines (54 loc) • 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useCells = useCells; var _react = require("react"); var _Cell = require("../component/Cell"); var _ReactUtils = require("./ReactUtils"); var _propsAreEqual = require("./propsAreEqual"); /** * A single shared reference for "this graphical item has no Cell children", * which is by far the most common case. */ var noCells = []; function cellsAreEqual(a, b) { if (a === b) { return true; } if (a.length !== b.length) { return false; } return a.every((cell, index) => { var other = b[index]; return other != null && cell.type === other.type && (0, _propsAreEqual.propsAreEqual)(cell.props, other.props); }); } /** * Reads the `Cell` children of a graphical item and returns a reference that is stable * for as long as the cells themselves do not change. * * `findAllByType` allocates a new array on every call, and JSX allocates new `Cell` elements * every time the parent renders. Cells are an argument to the memoized selectors that compute * the graphical item shapes, so an unstable reference here is a selector cache miss, which * produces a new array of shapes, which changes the animationId, which starts a new animation. * * That animation interpolates between shapes that hold identical values, so nothing appears to * move, but `isAnimating` still flips for the whole animationDuration. Labels are hidden while * a graphical item animates, so they blank out on every unrelated re-render of the surrounding * component - for example while the pointer moves over the chart. The animation callbacks fire * spuriously too, and every shape re-renders at frame rate for as long as it runs. * * @param children children of the graphical item, some of which may be `Cell` elements * @returns the Cell children; the same reference as last render if they are unchanged */ function useCells(children) { var previousRef = (0, _react.useRef)(noCells); var nextCells = (0, _ReactUtils.findAllByType)(children, _Cell.Cell); var cells = nextCells.length === 0 ? noCells : nextCells; if (cellsAreEqual(previousRef.current, cells)) { return previousRef.current; } previousRef.current = cells; return cells; }