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

[core] Add React 18 to peer dependencies #4332

Merged
merged 3 commits into from
Apr 11, 2022
Merged
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
2 changes: 1 addition & 1 deletion docs/data/data-grid/events/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
"name": "rowsScroll",
"description": "Fired during the scroll of the grid viewport.",
"params": "GridScrollParams",
"event": "MuiEvent<{}>"
"event": "MuiEvent<React.UIEvent | MuiBaseEvent>"
},
{
"name": "rowsScrollEnd",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/components/data-grid/events/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
"name": "rowsScroll",
"description": "Fired during the scroll of the grid viewport.",
"params": "GridScrollParams",
"event": "MuiEvent<{}>"
"event": "MuiEvent<React.UIEvent | MuiBaseEvent>"
},
{
"name": "rowsScrollEnd",
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/x-data-grid-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"peerDependencies": {
"@mui/icons-material": "^5.2.5",
"@mui/material": "^5.2.8",
"react": "^17.0.2"
"react": "^17.0.2 || ^18.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/x-data-grid-pro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"peerDependencies": {
"@mui/material": "^5.2.8",
"@mui/system": "^5.2.8",
"react": "^17.0.2"
"react": "^17.0.2 || ^18.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,15 @@ export const DataGridProColumnHeaders = React.forwardRef<
const pinnedColumns = useGridSelector(apiRef, gridPinnedColumnsSelector);
const [leftPinnedColumns, rightPinnedColumns] = filterColumns(pinnedColumns, visibleColumnFields);

const {
isDragging,
renderContext,
updateInnerPosition,
getRootProps,
getInnerProps,
getColumns,
} = useGridColumnHeaders({
innerRef,
minColumnIndex: leftPinnedColumns.length,
});
const { isDragging, renderContext, getRootProps, getInnerProps, getColumns } =
useGridColumnHeaders({
innerRef,
minColumnIndex: leftPinnedColumns.length,
});

const ownerState = { leftPinnedColumns, rightPinnedColumns, classes: rootProps.classes };
const classes = useUtilityClasses(ownerState);

React.useEffect(() => {
if (renderContext) {
updateInnerPosition(renderContext);
}
}, [renderContext, updateInnerPosition]);

const leftRenderContext =
renderContext && leftPinnedColumns.length
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ const DataGridProVirtualScroller = React.forwardRef<
useGridApiEventHandler(apiRef, GridEvents.columnWidthChange, refreshRenderZonePosition);
useGridApiEventHandler(apiRef, GridEvents.columnOrderChange, refreshRenderZonePosition);

React.useEffect(() => {
refreshRenderZonePosition();
}, [refreshRenderZonePosition]);

const leftRenderContext =
renderContext && leftPinnedColumns.length > 0
? {
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/x-data-grid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"peerDependencies": {
"@mui/material": "^5.2.8",
"@mui/system": "^5.2.8",
"react": "^17.0.2"
"react": "^17.0.2 || ^18.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useForkRef } from '@mui/material/utils';
import { useGridApiContext } from '../../utils/useGridApiContext';
import { useGridSelector } from '../../utils/useGridSelector';
Expand Down Expand Up @@ -26,6 +27,10 @@ interface UseGridColumnHeadersProps {
minColumnIndex?: number;
}

function isUIEvent(event: any): event is React.UIEvent {
return !!event.target;
}

export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
const { innerRef: innerRefProp, minColumnIndex = 0 } = props;

Expand Down Expand Up @@ -70,8 +75,14 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
[columnPositions, minColumnIndex, rootProps.columnBuffer],
);

React.useLayoutEffect(() => {
if (renderContext) {
updateInnerPosition(renderContext);
}
}, [renderContext, updateInnerPosition]);

const handleScroll = React.useCallback<GridEventListener<GridEvents.rowsScroll>>(
({ left, renderContext: nextRenderContext = null }) => {
({ left, renderContext: nextRenderContext = null }, event) => {
if (!innerRef.current) {
return;
}
Expand All @@ -87,13 +98,30 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
}
prevScrollLeft.current = left;

// We can only update the position when we guarantee that the render context has been
// rendered. This is achieved using ReactDOM.flushSync or when the context doesn't change.
let canUpdateInnerPosition = false;

if (nextRenderContext !== prevRenderContext.current || !prevRenderContext.current) {
setRenderContext(nextRenderContext);
// ReactDOM.flushSync cannot be called on `scroll` events fired inside effects
if (isUIEvent(event)) {
// To prevent flickering, the inner position can only be updated after the new context has
// been rendered. ReactDOM.flushSync ensures that the state changes will happen before
// updating the position.
ReactDOM.flushSync(() => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes

msedge_c95qQZ4C70

setRenderContext(nextRenderContext);
});
canUpdateInnerPosition = true;
} else {
setRenderContext(nextRenderContext);
}
prevRenderContext.current = nextRenderContext;
} else {
canUpdateInnerPosition = true;
}

// Pass directly the render context to avoid waiting for the next render
if (nextRenderContext) {
if (nextRenderContext && canUpdateInnerPosition) {
updateInnerPosition(nextRenderContext);
}
},
Expand Down Expand Up @@ -204,7 +232,6 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
renderContext,
getColumns,
isDragging: !!dragCol,
updateInnerPosition,
getRootProps: (other = {}) => ({ style: rootStyle, ...other }),
getInnerProps: () => ({ ref: handleInnerRef, 'aria-rowindex': 1, role: 'row' }),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useForkRef } from '@mui/material/utils';
import { useGridApiContext } from '../../utils/useGridApiContext';
import { useGridRootProps } from '../../utils/useGridRootProps';
Expand Down Expand Up @@ -197,13 +198,18 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
],
);

React.useLayoutEffect(() => {
if (renderContext) {
updateRenderZonePosition(renderContext);
}
}, [renderContext, updateRenderZonePosition]);

const updateRenderContext = React.useCallback(
(nextRenderContext) => {
setRenderContext(nextRenderContext);
updateRenderZonePosition(nextRenderContext);
prevRenderContext.current = nextRenderContext;
},
[setRenderContext, prevRenderContext, updateRenderZonePosition],
[setRenderContext, prevRenderContext],
);

React.useEffect(() => {
Expand All @@ -212,7 +218,6 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
}

const initialRenderContext = computeRenderContext();
prevRenderContext.current = initialRenderContext;
updateRenderContext(initialRenderContext);

const { top, left } = scrollPosition.current!;
Expand Down Expand Up @@ -257,14 +262,21 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
prevTotalWidth.current !== columnsTotalWidth;

// TODO v6: rename event to a wider name, it's not only fired for row scrolling
apiRef.current.publishEvent(GridEvents.rowsScroll, {
top: scrollTop,
left: scrollLeft,
renderContext: shouldSetState ? nextRenderContext : prevRenderContext.current,
});
apiRef.current.publishEvent(
GridEvents.rowsScroll,
{
top: scrollTop,
left: scrollLeft,
renderContext: shouldSetState ? nextRenderContext : prevRenderContext.current,
},
event,
);

if (shouldSetState) {
updateRenderContext(nextRenderContext);
// Prevents batching render context changes
ReactDOM.flushSync(() => {
updateRenderContext(nextRenderContext);
Copy link
Member Author

@m4theushw m4theushw Apr 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes the following glitch:

msedge_Z3qsBKGClx

From what I found, React 18 doesn't commit some renders if they take too much time. But, no matter the case, we update the render zone position so the position ends not matching the context rendered. Flushing the tree ensures that no render will be skipped. facebook/react#18402 (comment)

Updating the position inside React.useLayoutEffect also didn't work quite well. Here's the result:

msedge_ARUYFaWVCh

I would like to see how other virtualization libraries will solve this problem. Until there we have to use this workaround. If you have suggestions to improve this please comment.

});
prevTotalWidth.current = columnsTotalWidth;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export interface GridEventLookup
};

// Scroll
rowsScroll: { params: GridScrollParams };
rowsScroll: { params: GridScrollParams; event: React.UIEvent | MuiBaseEvent };
virtualScrollerContentSizeChange: {};

// Selection
Expand Down
4 changes: 2 additions & 2 deletions packages/x-date-pickers-pro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
"dayjs": "^1.10.7",
"luxon": "^1.28.0 || ^2.0.0",
"moment": "^2.29.1",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react": "^17.0.2 || ^18.0.0",
"react-dom": "^17.0.2 || ^18.0.0"
},
"peerDependenciesMeta": {
"date-fns": {
Expand Down
4 changes: 2 additions & 2 deletions packages/x-date-pickers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
"dayjs": "^1.10.7",
"luxon": "^1.28.0 || ^2.0.0",
"moment": "^2.29.1",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react": "^17.0.2 || ^18.0.0",
"react-dom": "^17.0.2 || ^18.0.0"
},
"peerDependenciesMeta": {
"date-fns": {
Expand Down
2 changes: 1 addition & 1 deletion packages/x-license-pro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"yargs": "^17.4.1"
},
"peerDependencies": {
"react": "^17.0.2"
"react": "^17.0.2 || ^18.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down