From 7e982e4c4ec7532b86e3728be900e30da96e6a41 Mon Sep 17 00:00:00 2001 From: Marija Najdova Date: Fri, 24 Jun 2022 09:52:42 +0200 Subject: [PATCH] [Tabs] Fix crash when used with React 18 & Suspense --- packages/mui-material/src/Tabs/Tabs.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/mui-material/src/Tabs/Tabs.js b/packages/mui-material/src/Tabs/Tabs.js index d9a85d0668a69e..a9623d9f7afc25 100644 --- a/packages/mui-material/src/Tabs/Tabs.js +++ b/packages/mui-material/src/Tabs/Tabs.js @@ -562,8 +562,16 @@ const Tabs = React.forwardRef(function Tabs(inProps, ref) { React.useEffect(() => { const handleResize = debounce(() => { - updateIndicatorState(); - updateScrollButtonState(); + // If the Tabs component is replaced by Suspense with a fallback, the last + // ResizeObserver's handler that runs because of the change in the layout is trying to + // access a dom node that is no longer there (as the fallback component is being shown instead). + // See https://github.com/mui/material-ui/issues/33276 + // TODO: Add tests that will ensure the component is not failing when + // replaced by Suspense with a fallback, once React is updated to version 18 + if (tabsRef.current) { + updateIndicatorState(); + updateScrollButtonState(); + } }); const win = ownerWindow(tabsRef.current); win.addEventListener('resize', handleResize);