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

Fix(#2941): hydration error using victory line on next js 15 #2973

Merged
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
5 changes: 5 additions & 0 deletions .changeset/wicked-pandas-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-core": patch
---

fix hydration error using victory line with nextjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from "react";
import defaults from "lodash/defaults";
import isObject from "lodash/isObject";
import uniqueId from "lodash/uniqueId";

import * as Helpers from "../victory-util/helpers";
Expand Down Expand Up @@ -32,7 +31,14 @@ export interface VictoryClipContainerProps {
translateY?: number;
}

export class VictoryClipContainer extends React.Component<VictoryClipContainerProps> {
interface VictoryClipContainerState {
clipId?: number | string;
}

export class VictoryClipContainer extends React.Component<
VictoryClipContainerProps,
VictoryClipContainerState
> {
static displayName = "VictoryClipContainer";
static role = "container";

Expand All @@ -46,10 +52,18 @@ export class VictoryClipContainer extends React.Component<VictoryClipContainerPr

constructor(props: VictoryClipContainerProps) {
super(props);
this.clipId =
!isObject(props) || props.clipId === undefined
? uniqueId("victory-clip-")
: props.clipId;
this.state = {
clipId: props?.clipId,
};
}

// The clipId state is used to prevent hydration mismatches between the server and client (issue #2941).
// A better alternative would be to utilize React 18's useId hook instead of uniqueId, which would avoid needing state for this purpose.
// However, this component currently supports React 16 at the time of writing, so this workaround is necessary.
componentDidMount() {
if (!this.state.clipId) {
this.setState({ clipId: uniqueId("victory-clip-") });
}
}

calculateAttributes(props) {
Expand Down Expand Up @@ -204,6 +218,6 @@ export class VictoryClipContainer extends React.Component<VictoryClipContainerPr
translateX,
translateY,
});
return this.renderClippedGroup(clipProps, this.clipId);
return this.renderClippedGroup(clipProps, this.state.clipId);
}
}
Loading