Skip to content

Commit

Permalink
commented on an anti-pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall committed Nov 20, 2019
1 parent 05c7f10 commit 4cbb9fa
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/components/datagrid/data_grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,7 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = props => {
</EuiI18n>
);

const [cellsUpdateFocus, setCellsUpdateFocus] = useState<
Map<string, Function>
>(new Map());
const [cellsUpdateFocus] = useState<Map<string, Function>>(new Map());

const updateFocus = (focusedCell: [number, number]) => {
const key = `${focusedCell[0]}-${focusedCell[1]}`;
Expand All @@ -616,16 +614,13 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = 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);
};
}
},
Expand Down

0 comments on commit 4cbb9fa

Please sign in to comment.