Skip to content

Commit 73ca10c

Browse files
committed
fix(ts): add default props to previous props
This avoids an infinite useEffect/setState loop via the modified flag.
1 parent a373b8f commit 73ca10c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/core-ts/DataArray.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
2-
import { useEffect } from 'react';
3-
import { usePrevious } from '../utils-ts/usePrevious';
2+
import { vtkRange } from '@kitware/vtk.js/interfaces';
43
import { TYPED_ARRAYS } from '@kitware/vtk.js/macros';
5-
import { useDataset, useFieldData } from './contexts';
6-
import { toTypedArray, TypedArrayLookup } from '../utils-ts';
4+
import { useEffect } from 'react';
75
import { DataArrayValues } from '../types';
8-
import { vtkRange } from '@kitware/vtk.js/interfaces';
9-
import useUnmount from '../utils-ts/useUnmount';
10-
import { useOrderedUnmountEffect } from '../utils-ts/useOrderedUnmountEffect';
6+
import { toTypedArray, TypedArrayLookup } from '../utils-ts';
117
import useGetterRef from '../utils-ts/useGetterRef';
12-
13-
// -----
8+
import { useOrderedUnmountEffect } from '../utils-ts/useOrderedUnmountEffect';
9+
import { usePrevious } from '../utils-ts/usePrevious';
10+
import useUnmount from '../utils-ts/useUnmount';
11+
import { useDataset, useFieldData } from './contexts';
1412

1513
interface Props {
1614
/**
@@ -52,7 +50,7 @@ const DefaultProps = {
5250
};
5351

5452
export default function DataArray(props: Props) {
55-
const prev = usePrevious(props);
53+
const prev = usePrevious({ ...DefaultProps, ...props });
5654

5755
const [daRef, getDataArray] = useGetterRef(() =>
5856
vtkDataArray.newInstance({

src/core-ts/PolyData.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,22 @@ interface Props extends PropsWithChildren {
8585
connectivity?: string;
8686
}
8787

88+
const DefaultProps = {
89+
port: 0,
90+
points: [],
91+
connectivity: 'manual',
92+
};
93+
8894
export default forwardRef(function PolyData(props: Props, fwdRef) {
8995
const {
90-
port = 0,
91-
points = [],
92-
connectivity = 'manual',
96+
port = DefaultProps.port,
97+
points = DefaultProps.points,
98+
connectivity = DefaultProps.connectivity,
9399
verts,
94100
lines,
95101
polys,
96102
strips,
97103
} = props;
98-
const prev = usePrevious(props);
99104

100105
const [pdRef, getPolyData] = useGetterRef(() => vtkPolyData.newInstance());
101106

@@ -126,6 +131,8 @@ export default forwardRef(function PolyData(props: Props, fwdRef) {
126131

127132
useImperativeHandle(fwdRef, () => dataset);
128133

134+
const prev = usePrevious({ ...DefaultProps, ...props });
135+
129136
// update polydata
130137
useEffect(() => {
131138
const polyData = getPolyData();

0 commit comments

Comments
 (0)