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

chore: spike - improve performance with early return #3

Open
wants to merge 1 commit into
base: bwittenberg/3951
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion packages/react/src/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ function DataTable<Data extends UniqueRow>({
initialSortColumn,
initialSortDirection,
}: DataTableProps<Data>) {
const {headers, rows, actions, gridTemplateColumns} = useTable({
const {headers, rows, actions, gridTemplateColumns, willReactThrowOutRenderResult} = useTable({
data,
columns,
initialSortColumn,
initialSortDirection,
})

if (willReactThrowOutRenderResult) {
return null
}

return (
<Table
aria-labelledby={labelledby}
Expand Down
28 changes: 27 additions & 1 deletion packages/react/src/DataTable/__tests__/DataTable.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import userEvent from '@testing-library/user-event'
import {render, screen, queryAllByRole} from '@testing-library/react'
import React from 'react'
import React, {Profiler, type ComponentProps} from 'react'
import {DataTable, Table} from '../../DataTable'
import type {Column} from '../column'
import {createColumnHelper} from '../column'
Expand Down Expand Up @@ -1036,4 +1036,30 @@ describe('DataTable', () => {
})
})
})

describe('performance tests', () => {
it('should not render twice on initial render', () => {
type Data = {id: string; value: string}
const columns: ComponentProps<typeof DataTable<Data>>['columns'] = [
{
header: 'Value',
field: 'value',
},
]
const data: ComponentProps<typeof DataTable<Data>>['data'] = [
{
id: '1',
value: 'one',
},
]
let renderCount = 0
render(
<Profiler id="DataTable" onRender={() => renderCount++}>
<DataTable data={data} columns={columns} />
</Profiler>,
)

expect(renderCount).toBe(1)
})
})
})
9 changes: 6 additions & 3 deletions packages/react/src/DataTable/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function useTable<Data extends UniqueRow>({
data,
initialSortColumn,
initialSortDirection,
}: TableConfig<Data>): Table<Data> {
}: TableConfig<Data>): Table<Data> & {willReactThrowOutRenderResult: boolean} {
const [rowOrder, setRowOrder] = useState(data)
const [prevData, setPrevData] = useState<typeof data | null>(null)
const [prevColumns, setPrevColumns] = useState(columns)
Expand All @@ -58,7 +58,8 @@ export function useTable<Data extends UniqueRow>({

// Reset the `sortByColumn` state if the columns change and that column is no
// longer provided
if (columns !== prevColumns) {
const haveColumnsChanged = columns !== prevColumns
if (haveColumnsChanged) {
setPrevColumns(columns)
if (sortByColumn) {
const column = columns.find(column => {
Expand Down Expand Up @@ -94,7 +95,8 @@ export function useTable<Data extends UniqueRow>({
})

// Update the row order and apply the current sort column to the incoming data
if (data !== prevData) {
const hasDataChanged = data !== prevData
if (hasDataChanged) {
setPrevData(data)
setRowOrder(data)
if (sortByColumn) {
Expand Down Expand Up @@ -207,6 +209,7 @@ export function useTable<Data extends UniqueRow>({
sortBy,
},
gridTemplateColumns,
willReactThrowOutRenderResult: hasDataChanged || haveColumnsChanged,
}
}

Expand Down