Skip to content

Commit

Permalink
Memoized callback in useTableSize (deephaven#1928)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed May 13, 2024
1 parent 28a9b2f commit 6595850
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/jsapi-components/src/useTableSize.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, renderHook } from '@testing-library/react-hooks';
import dh from '@deephaven/jsapi-shim';
import type { Table } from '@deephaven/jsapi-types';
import type { dh as DhType } from '@deephaven/jsapi-types';
import { TestUtils } from '@deephaven/utils';
import useTableSize from './useTableSize';
import useTableListener from './useTableListener';
Expand All @@ -21,7 +21,7 @@ it.each([null, undefined])('should return 0 if no table', table => {

it('should return the size of the given table', () => {
const size = 10;
const table = TestUtils.createMockProxy<Table>({ size });
const table = TestUtils.createMockProxy<DhType.Table>({ size });

const { result } = renderHook(() => useTableSize(table), { wrapper });

Expand All @@ -33,7 +33,7 @@ it('should re-render if dh.Table.EVENT_SIZECHANGED event occurs', () => {
const table = {
addEventListener: jest.fn(),
size: initialSize,
} as unknown as Table;
} as unknown as DhType.Table;

const { result } = renderHook(() => useTableSize(table), { wrapper });

Expand Down
8 changes: 5 additions & 3 deletions packages/jsapi-components/src/useTableSize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useCallback, useState } from 'react';
import { useApi } from '@deephaven/jsapi-bootstrap';
import type { dh } from '@deephaven/jsapi-types';
import { getSize } from '@deephaven/jsapi-utils';
Expand All @@ -18,9 +18,11 @@ export function useTableSize(

const dh = useApi();

useTableListener(table, dh.Table.EVENT_SIZECHANGED, () => {
const onSizeChanged = useCallback(() => {
forceRerender(i => i + 1);
});
}, []);

useTableListener(table, dh.Table.EVENT_SIZECHANGED, onSizeChanged);

return getSize(table);
}
Expand Down

0 comments on commit 6595850

Please sign in to comment.