From 4cbb9fad513dd9b611b191705befcc6a2073f34c Mon Sep 17 00:00:00 2001 From: Chandler Prall Date: Wed, 20 Nov 2019 09:00:07 -0700 Subject: [PATCH] commented on an anti-pattern --- src/components/datagrid/data_grid.tsx | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/components/datagrid/data_grid.tsx b/src/components/datagrid/data_grid.tsx index ac1de404e27..8c5548195da 100644 --- a/src/components/datagrid/data_grid.tsx +++ b/src/components/datagrid/data_grid.tsx @@ -598,9 +598,7 @@ export const EuiDataGrid: FunctionComponent = props => { ); - const [cellsUpdateFocus, setCellsUpdateFocus] = useState< - Map - >(new Map()); + const [cellsUpdateFocus] = useState>(new Map()); const updateFocus = (focusedCell: [number, number]) => { const key = `${focusedCell[0]}-${focusedCell[1]}`; @@ -616,16 +614,13 @@ export const EuiDataGrid: FunctionComponent = props => { if (pagination) { const key = `${cell[0]}-${cell[1]}`; - setCellsUpdateFocus(cellsUpdateFocus => { - cellsUpdateFocus.set(key, updateFocus); - return cellsUpdateFocus; - }); + // this intentionally and purposefully mutates the existing `cellsUpdateFocus` object as the + // value/state of `cellsUpdateFocus` must be up-to-date when `updateFocus`'s requestAnimationFrame fires + // there is likely a better pattern to use, but this is fine for now as the scope is known & limited + cellsUpdateFocus.set(key, updateFocus); return () => { - setCellsUpdateFocus(cellsUpdateFocus => { - cellsUpdateFocus.delete(key); - return cellsUpdateFocus; - }); + cellsUpdateFocus.delete(key); }; } },