-
Notifications
You must be signed in to change notification settings - Fork 789
fix(ScrollControls): cache ReactDOM root for React 19 compatibility #2557
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,7 +236,32 @@ const ScrollHtml: ForwardRefComponent<{ children?: React.ReactNode; style?: Reac | |
| React.useImperativeHandle(ref, () => group.current, []) | ||
| const { width, height } = useThree((state) => state.size) | ||
| const fiberState = React.useContext(fiberContext) | ||
| const root = React.useMemo(() => ReactDOM.createRoot(state.fixed), [state.fixed]) | ||
| // Cache a root instance on the fixed DOM element to avoid calling | ||
| // ReactDOM.createRoot multiple times on the same container which | ||
| // triggers an error in React 19. We store the created root in a | ||
| // non-enumerable property on the element so repeated mounts or | ||
| // remounts re-use the same root instance. | ||
| const root = React.useMemo(() => { | ||
| try { | ||
| // If a root was previously created on this element, reuse it. | ||
| if (state.fixed && (state.fixed as any).__r3_root) return (state.fixed as any).__r3_root | ||
|
||
| const r = ReactDOM.createRoot(state.fixed) | ||
| try { | ||
| Object.defineProperty(state.fixed, '__r3_root', { | ||
| value: r, | ||
| configurable: true, | ||
| writable: false, | ||
| }) | ||
|
||
| } catch (e) { | ||
| // ignore if we can't define property (very unlikely) | ||
| ;(state.fixed as any).__r3_root = r | ||
| } | ||
| return r | ||
| } catch (e) { | ||
| // Fallback: if createRoot fails for any reason, rethrow so devs see it. | ||
| throw e | ||
| } | ||
| }, [state.fixed]) | ||
| useFrame(() => { | ||
| if (state.delta > state.eps) { | ||
| group.current.style.transform = `translate3d(${ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you update useThree types to include state.fixed and tag me for review again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
statevariable used in the root caching logic (lines 245-264) comes fromuseScroll()on line 234, which returnsScrollControlsState(already typed withfixed: HTMLDivElementon line 37).useThree()call only extractssizefor layout calculations and is unrelated to thestate.fixedproperty being accessed below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See R247