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 maximum update depth exceeded #387

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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: 3 additions & 4 deletions src/Spreadsheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getCSV,
shouldHandleClipboardEvent,
isFocusedWithin,
deepArrayComparison,
} from "./util";

import DefaultTable from "./Table";
Expand Down Expand Up @@ -235,13 +236,12 @@ const Spreadsheet = <CellType extends Types.CellBase>(
// Listen to data changes
const prevDataRef = React.useRef<Matrix.Matrix<CellType>>(state.model.data);
React.useEffect(() => {
if (state.model.data !== prevDataRef.current) {
if (!deepArrayComparison(state.model.data, prevDataRef.current)) {
// Call on change only if the data change internal
if (state.model.data !== props.data) {
if (!deepArrayComparison(state.model.data, props.data)) {
Copy link
Owner

Choose a reason for hiding this comment

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

I'm sorry but although this is a working solution for small datasets, for big ones it could be very problematic. The fix should be to make sure the reference isn't different in the first place.

onChange(state.model.data);
}
}

prevDataRef.current = state.model.data;
}, [state.model.data, onChange, props.data]);

Expand All @@ -252,7 +252,6 @@ const Spreadsheet = <CellType extends Types.CellBase>(
if (state?.model?.evaluatedData !== prevEvaluatedDataRef?.current) {
onEvaluatedDataChange(state?.model?.evaluatedData);
}

prevEvaluatedDataRef.current = state.model.evaluatedData;
}, [state?.model?.evaluatedData, onEvaluatedDataChange]);

Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ export function range(end: number, start = 0, step = 1): number[] {
return array;
}

/** Perform deep comparison of two (nested) arrays. */
export function deepArrayComparison(arr1: any[], arr2: any[]): boolean {
if (arr1.length !== arr2.length) return false;
return arr1.every((element, index) => {
if (Array.isArray(element) && Array.isArray(arr2[index])) {
return deepArrayComparison(element, arr2[index]);
}
return element === arr2[index];
});
}

/** Return whether given point is active */
export function isActive(
active: Types.StoreState["active"],
Expand Down