Skip to content

Commit

Permalink
[DataGrid] Experiment without raf
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Oct 28, 2020
1 parent f1835be commit 2ce1ac9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import * as React from 'react';
import { ApiRef } from '../../../models/api/apiRef';
import { useRafUpdate } from '../../utils/useRafUpdate';
import { GridState } from './gridState';
import { useGridApi } from './useGridApi';

export const useGridState = (
apiRef: ApiRef,
): [GridState, (stateUpdaterFn: (oldState: GridState) => GridState) => void, () => void] => {
const api = useGridApi(apiRef);
const [, forceUpdate] = React.useState();
const [rafUpdate] = useRafUpdate(() => forceUpdate((p: any) => !p));
const [, forceUpdate] = React.useReducer((acc) => acc + 1, 0);

const setGridState = React.useCallback(
(stateUpdaterFn: (oldState: GridState) => GridState) => {
Expand All @@ -18,5 +16,5 @@ export const useGridState = (
[api],
);

return [api.state, setGridState, rafUpdate];
return [api.state, setGridState, forceUpdate];
};
1 change: 0 additions & 1 deletion packages/grid/_modules_/grid/hooks/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './useLogger';
export * from './useRafUpdate';
export * from './useScrollFn';
22 changes: 8 additions & 14 deletions packages/grid/_modules_/grid/hooks/utils/useScrollFn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { debounce } from '@material-ui/core/utils';
import { useLogger } from './useLogger';
import { useRafUpdate } from './useRafUpdate';

export interface ScrollParams {
left: number;
Expand All @@ -14,18 +13,15 @@ export function useScrollFn(
onScroll?: ScrollFn,
): [ScrollFn, ScrollFn] {
const logger = useLogger('useScrollFn');
const rafResetPointerRef = React.useRef(0);
const previousValue = React.useRef<ScrollParams>();
const [restorePointerEvents] = useRafUpdate(() => {
if (scrollingElementRef && scrollingElementRef.current) {
scrollingElementRef.current!.style.pointerEvents = 'unset';
}
rafResetPointerRef.current = 0;
});

const debouncedResetPointerEvents = React.useMemo(() => debounce(restorePointerEvents, 300), [
restorePointerEvents,
]);
const debouncedResetPointerEvents = React.useMemo(
() =>
debounce(() => {
scrollingElementRef.current!.style.pointerEvents = 'unset';
}, 300),
[scrollingElementRef],
);

const scrollTo: (v: ScrollParams) => void = React.useCallback(
(v) => {
Expand All @@ -50,13 +46,11 @@ export function useScrollFn(
[scrollingElementRef, debouncedResetPointerEvents, logger, onScroll],
);

const [runScroll] = useRafUpdate(scrollTo);

React.useEffect(() => {
return () => {
debouncedResetPointerEvents.clear();
};
}, [scrollingElementRef, debouncedResetPointerEvents]);

return [runScroll, scrollTo];
return [scrollTo, scrollTo];
}

0 comments on commit 2ce1ac9

Please sign in to comment.