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

Assign merged props to a const instead of modifying initialProps #2718

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .changeset/angry-pens-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"victory-chart": patch
"victory-native": patch
"victory-stack": patch
---

Assign merged props to a const instead of modifying initialProps
19 changes: 11 additions & 8 deletions packages/victory-chart/src/victory-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ const defaultProps = {
};

const VictoryChartImpl: React.FC<VictoryChartProps> = (initialProps) => {
initialProps = { ...defaultProps, ...initialProps };
const propsWithDefaults = React.useMemo(
() => ({ ...defaultProps, ...initialProps }),
[initialProps],
);
const role = "chart";
const { getAnimationProps, setAnimationState, getProps } =
Hooks.useAnimationState();
const props = getProps(initialProps);
const props = getProps(propsWithDefaults);

const modifiedProps = Helpers.modifyProps(props, fallbackProps, role);
const {
Expand Down Expand Up @@ -154,7 +157,7 @@ const VictoryChartImpl: React.FC<VictoryChartProps> = (initialProps) => {
{},
containerComponent.props,
containerProps,
UserProps.getSafeUserProps(initialProps),
UserProps.getSafeUserProps(propsWithDefaults),
);
return React.cloneElement(containerComponent, defaultContainerProps);
}
Expand All @@ -164,23 +167,23 @@ const VictoryChartImpl: React.FC<VictoryChartProps> = (initialProps) => {
standalone,
containerComponent,
containerProps,
initialProps,
propsWithDefaults,
]);

const events = React.useMemo(() => {
return Wrapper.getAllEvents(props);
}, [props]);

const previousProps = Hooks.usePreviousProps(initialProps);
const previousProps = Hooks.usePreviousProps(propsWithDefaults);

React.useEffect(() => {
// This is called before dismount to keep state in sync
return () => {
if (initialProps.animate) {
setAnimationState(previousProps, initialProps);
if (propsWithDefaults.animate) {
setAnimationState(previousProps, propsWithDefaults);
}
};
}, [setAnimationState, previousProps, initialProps]);
}, [setAnimationState, previousProps, propsWithDefaults]);

if (!isEmpty(events)) {
return (
Expand Down
4 changes: 2 additions & 2 deletions packages/victory-native/src/helpers/wrap-core-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import React from "react";
*/
export const wrapCoreComponent = ({ Component, defaultProps }) => {
const WrappedComponent = (props) => {
props = { ...defaultProps, ...props };
return <Component {...props} />;
const propsWithDefaults = { ...defaultProps, ...props };
return <Component {...propsWithDefaults} />;
};

/**
Expand Down
20 changes: 11 additions & 9 deletions packages/victory-stack/src/victory-stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ const defaultProps = {
};

const VictoryStackBase = (initialProps: VictoryStackProps) => {
// eslint-disable-next-line no-use-before-define
const { role } = VictoryStack;
initialProps = { ...defaultProps, ...initialProps };
const propsWithDefaults = React.useMemo(
() => ({ ...defaultProps, ...initialProps }),
[initialProps],
);
const { setAnimationState, getAnimationProps, getProps } =
Hooks.useAnimationState();

const props = getProps(initialProps);
const props = getProps(propsWithDefaults);

const modifiedProps = Helpers.modifyProps(props, fallbackProps, role);
const {
Expand Down Expand Up @@ -134,8 +136,8 @@ const VictoryStackBase = (initialProps: VictoryStackProps) => {
name,
]);
const userProps = React.useMemo(
() => UserProps.getSafeUserProps(initialProps),
[initialProps],
() => UserProps.getSafeUserProps(propsWithDefaults),
[propsWithDefaults],
);

const container = React.useMemo(() => {
Expand All @@ -161,16 +163,16 @@ const VictoryStackBase = (initialProps: VictoryStackProps) => {
return Wrapper.getAllEvents(props);
}, [props]);

const previousProps = Hooks.usePreviousProps(initialProps);
const previousProps = Hooks.usePreviousProps(propsWithDefaults);

React.useEffect(() => {
// This is called before dismount to keep state in sync
return () => {
if (initialProps.animate) {
setAnimationState(previousProps, initialProps);
if (propsWithDefaults.animate) {
setAnimationState(previousProps, propsWithDefaults);
}
};
}, [setAnimationState, previousProps, initialProps]);
}, [setAnimationState, previousProps, propsWithDefaults]);

if (!isEmpty(events)) {
return (
Expand Down