Skip to content

Commit 9ea702f

Browse files
authored
fix(react): React Compiler strips memoization and causes MenuPortal to re-mount (#2077)
### 💡 Overview This change removes the use of a randomly generated portal ID and instead uses a ref backed DOM element as the FloatingPortal root. Using Math.random() during render is not recommended and causes issues with some versions of React Compiler since the compiler may strip or reorder memoization.
1 parent be82657 commit 9ea702f

File tree

4 files changed

+6
-7
lines changed

4 files changed

+6
-7
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export default [
5050
'react/no-unescaped-entities': 'off',
5151
'react/react-in-jsx-scope': 'off',
5252
'react-hooks/rules-of-hooks': 'error',
53+
'react-hooks/purity': 'error',
5354
'react-hooks/exhaustive-deps': 'error',
5455
'react-compiler/react-compiler': 'off',
5556
},

packages/react-sdk/src/components/Menu/MenuToggle.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
RefAttributes,
66
useContext,
77
useEffect,
8-
useMemo,
98
useRef,
109
useState,
1110
} from 'react';
@@ -59,16 +58,13 @@ const MenuPortal = ({
5958
}: PropsWithChildren<{
6059
refs: UseFloatingReturn['refs'];
6160
}>) => {
62-
const portalId = useMemo(
63-
() => `str-video-portal-${Math.random().toString(36).substring(2, 9)}`,
64-
[],
65-
);
61+
const [portalRoot, setPortalRoot] = useState<HTMLDivElement | null>(null);
6662

6763
return (
6864
<>
69-
<div id={portalId} className="str-video__portal" />
65+
<div ref={setPortalRoot} className="str-video__portal" />
7066
<FloatingOverlay>
71-
<FloatingPortal id={portalId}>
67+
<FloatingPortal root={portalRoot}>
7268
<div className="str-video__portal-content" ref={refs.setFloating}>
7369
{children}
7470
</div>

sample-apps/react/react-dogfood/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@types/react": "~19.1.17",
4646
"@types/react-dom": "~19.1.11",
4747
"@types/yargs": "^17.0.33",
48+
"babel-plugin-react-compiler": "^1.0.0",
4849
"rimraf": "^6.0.1",
4950
"sass": "^1.93.2"
5051
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7607,6 +7607,7 @@ __metadata:
76077607
"@types/react-dom": "npm:~19.1.11"
76087608
"@types/yargs": "npm:^17.0.33"
76097609
axios: "npm:^1.12.2"
7610+
babel-plugin-react-compiler: "npm:^1.0.0"
76107611
clsx: "npm:^2.0.0"
76117612
dotenv: "npm:^16.6.1"
76127613
framer-motion: "npm:^12.23.24"

0 commit comments

Comments
 (0)