Skip to content

Conversation

@zuiidea
Copy link

@zuiidea zuiidea commented Oct 27, 2025

👀 PR includes

🐛 Bugfix

📝 Description

Fixing createRoot reference in react-dom@19

🖼️ Screenshot

Before After
image

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @zuiidea, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical compatibility issue with react-dom@19 by correcting the reference to the createRoot function. The change ensures that applications leveraging newer React versions can correctly initialize their root components, thereby preventing potential rendering failures and enhancing the overall stability of the application when using React 18 or later.

Highlights

  • React 19 Compatibility: Updated the createRoot reference to ensure compatibility with react-dom@19 and React versions 18 and above.
  • Import Correction: Changed the import source for createRoot from ReactDOMClone to ReactDOMClient to align with modern React practices.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link
Contributor

你好 @zuiidea,非常感谢你的贡献.
为了让代码更健壮, 请根据 贡献指南 补充相应单元测试 (文档改动或其他特殊场景除外), 如果有 API 改动, 请修改 相应的文档.
同时请检查 linttest 相关 CI 是否通过, 如果失败, 请点击 [Details] 按钮查看, 并根据日志修复.

Hello, @zuiidea, Thanks for your contribution. In order to make the code more robust, please add the corresponding unit tests, and update the docs if there are API changes.
At the same time, please check whether the CI related to lint and test passes. If the CI fails, please click [Details] button to check and repair according to the log.

@github-actions github-actions bot added the next 2.0-next 版本的问题 label Oct 27, 2025
@github-actions github-actions bot requested a review from Alexzjt October 27, 2025 08:45
@github-actions github-actions bot added the pr(fix) bug fix label Oct 27, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request aims to fix the createRoot reference for React 18 and newer, which is a good improvement. However, the current implementation with a static import of react-dom/client will unfortunately break compatibility for users with React versions below 18, as the module won't be found. I've provided critical feedback with suggestions to use a dynamic require to resolve this, ensuring backward compatibility is maintained. Additionally, it appears the unit tests may need to be updated to correctly mock and verify the call to createRoot from react-dom/client.

import { version } from 'react';
import * as ReactDOM from 'react-dom';
import type { Root } from 'react-dom/client';
import * as ReactDOMClient from 'react-dom/client';
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This static import of react-dom/client will cause a module resolution error at runtime for projects using React versions older than 18, as this module path does not exist in those versions. This breaks the backward compatibility that this file aims to maintain. This import should be removed and createRoot should be loaded dynamically.


if (mainVersion >= 18) {
createRoot = ReactDOMClone.createRoot!;
createRoot = ReactDOMClient.createRoot!;
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

To conditionally load createRoot for React 18+ without breaking support for older versions, you should use a dynamic require() call here. The surrounding try...catch block will correctly handle the error if the react-dom/client module is not found. Please also remember to remove the static ReactDOMClient import from the top of the file.

Suggested change
createRoot = ReactDOMClient.createRoot!;
createRoot = require('react-dom/client').createRoot;

Copy link
Author

Choose a reason for hiding this comment

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

  • ({ createRoot } = await import('react-dom/client')) 这种写法可以在 react@18 及以上运行,但是在 react@17 时,因此模块不存在,所以vite:import-analysis 会报错

  • createRoot = require('react-dom/client').createRoot 这种写法 vite:import-analysis 不会报错,但是并不能动态引入模块,因此无法拿到 createRoot

@Alexzjt
Copy link
Contributor

Alexzjt commented Oct 28, 2025

有大量的已知业务还在使用 ~16.14.0。 所以低版本也是要支持的

@Alexzjt Alexzjt closed this Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

next 2.0-next 版本的问题 pr(fix) bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Uncaught TypeError: reactOriginalRender is not a function width React 19

2 participants