React 19 error: Update ScrollControls.tsx #2135
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why
I receive this error when upgrading to React @
19.0.0-rc-65a56d0e-20241020
You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before. Instead, call root.render() on the existing root instead if you want to update it.
To address the issue of calling ReactDOM.createRoot() on a container that has already been passed to createRoot(), we can store the root instance and reuse it for rendering updates. I'm not sure how to go about this with different versions of React.
What
Root Instance Storage: A rootRef is used to store the root instance. This ensures that ReactDOM.createRoot() is only called once, and the same root instance is reused for subsequent renders.
Conditional Initialization: The root is initialized only if rootRef.current is null, preventing multiple initializations.