Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize getCalculatedProps #2006

Merged
merged 8 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import get from "lodash/get";
import GlobalStyle from "./styles/global";
import theme from "./styles/theme";
import Analytics from "./google-analytics";
import { initGoogleTagManager } from './google-tag-manager';
import { initGoogleTagManager } from "./google-tag-manager";
import NotFound from "./pages/404";

// eslint-disable-next-line no-magic-numbers
Expand Down Expand Up @@ -85,7 +85,6 @@ ScrollToCurrentSection.propTypes = {

// eslint-disable-next-line react/no-multi-comp
const App = () => {

useEffect(() => {
initGoogleTagManager();
}, []);
Expand Down
6 changes: 3 additions & 3 deletions docs/src/google-tag-manager.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Google Tag Manager
*/
const TagManager = require('react-gtm-module');
const TagManager = require("react-gtm-module");

export const initGoogleTagManager = () => {
if (typeof document === 'undefined') {
if (typeof document === "undefined") {
return {};
} else {
return TagManager.initialize({ gtmId: 'GTM-MD32945' });
return TagManager.initialize({ gtmId: "GTM-MD32945" });
}
};
36 changes: 34 additions & 2 deletions packages/victory-group/src/helper-methods.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable func-style */
/* eslint-disable no-use-before-define */
import { assign } from "lodash";
import { assign, omit } from "lodash";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Victory maintains a simplified omit helper that is more performant than the more complete lodash.omit. You might consider using that instead: https://github.com/FormidableLabs/victory/blob/main/packages/victory-core/src/victory-util/helpers.js#L31

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

import React from "react";
import { Helpers, Scale, Data, Wrapper } from "victory-core";
import { Data, Helpers, Scale, Wrapper } from "victory-core";
import isEqual from "react-fast-compare";

const fallbackProps = {
width: 450,
Expand Down Expand Up @@ -63,6 +64,37 @@ export function getCalculatedProps(props, childComponents) {
};
}

// We need to remove sharedEvents in order to memoize the calculated data
// With shared events, the props change on every event, and every value is re-calculated
const withoutSharedEvents = (props) => {
const { children } = props;
const modifiedChildren = React.Children.toArray(children).map((child) => {
return {
...child,
props: omit(child.props, "sharedEvents")
};
});
props.children = modifiedChildren;
return props;
};

export function useMemoizedProps(initialProps) {
const modifiedProps = withoutSharedEvents(initialProps);
const [props, setProps] = React.useState(modifiedProps);

// React.memo uses shallow equality to compare objects. This way props
// will only be re-calculated when they change.
React.useEffect(() => {
if (!isEqual(modifiedProps, props)) {
setProps(modifiedProps);
}
}, [props, setProps, modifiedProps]);

return React.useMemo(() => {
return getCalculatedProps(props, props.children);
}, [props]);
}

function pixelsToValue(props, axis, calculatedProps) {
if (!props.offset) {
return 0;
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-group/src/victory-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
useAnimationState
} from "victory-core";
import { VictorySharedEvents } from "victory-shared-events";
import { getChildren, getCalculatedProps } from "./helper-methods";
import { getChildren, useMemoizedProps } from "./helper-methods";
import isEqual from "react-fast-compare";

const fallbackProps = {
Expand Down Expand Up @@ -44,7 +44,7 @@ const VictoryGroup = (initialProps) => {
} = modifiedProps;

const childComponents = React.Children.toArray(modifiedProps.children);
const calculatedProps = getCalculatedProps(modifiedProps, childComponents);
const calculatedProps = useMemoizedProps(modifiedProps);
const { domain, scale, style, origin } = calculatedProps;

const newChildren = React.useMemo(() => {
Expand Down
34 changes: 33 additions & 1 deletion packages/victory-stack/src/helper-methods.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable func-style */
/* eslint-disable no-use-before-define */
import { assign, keys, orderBy } from "lodash";
import { assign, keys, orderBy, omit } from "lodash";
import React from "react";
import { Helpers, Scale, Wrapper } from "victory-core";
import isEqual from "react-fast-compare";

const fallbackProps = {
width: 450,
Expand Down Expand Up @@ -160,6 +161,37 @@ export function getCalculatedProps(props, childComponents) {
};
}

// We need to remove sharedEvents in order to memoize the calculated data
// With shared events, the props change on every event, and every value is re-calculated
const withoutSharedEvents = (props) => {
const { children } = props;
const modifiedChildren = React.Children.toArray(children).map((child) => {
return {
...child,
props: omit(child.props, "sharedEvents")
};
});
props.children = modifiedChildren;
return props;
};

export function useMemoizedProps(initialProps) {
const modifiedProps = withoutSharedEvents(initialProps);
const [props, setProps] = React.useState(modifiedProps);

// React.memo uses shallow equality to compare objects. This way props
// will only be re-calculated when they change.
React.useEffect(() => {
if (!isEqual(modifiedProps, props)) {
setProps(modifiedProps);
}
}, [props, setProps, modifiedProps]);

return React.useMemo(() => {
return getCalculatedProps(props, props.children);
}, [props]);
}

function getLabels(props, datasets, index) {
if (!props.labels) {
return undefined;
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-stack/src/victory-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
usePreviousProps
} from "victory-core";
import { VictorySharedEvents } from "victory-shared-events";
import { getChildren, getCalculatedProps } from "./helper-methods";
import { getChildren, useMemoizedProps } from "./helper-methods";
import isEqual from "react-fast-compare";

const fallbackProps = {
Expand Down Expand Up @@ -45,7 +45,7 @@ const VictoryStack = (initialProps) => {
} = modifiedProps;

const childComponents = React.Children.toArray(modifiedProps.children);
const calculatedProps = getCalculatedProps(modifiedProps, childComponents);
const calculatedProps = useMemoizedProps(modifiedProps);
const { domain, scale, style, origin } = calculatedProps;

const newChildren = React.useMemo(() => {
Expand Down