Skip to content
Discussion options

You must be logged in to vote

在 React 18 之前,createRoot 仍然为保持兼容在 react-dom 里直接导出。在 React 19,createRoot 仅在 react-dom/client 中导出。所以无法兼容。

你或许会疑问,为什么不直接写两个:

import { render } from 'react-dom';
import { createRoot } from 'react-dom/clinet';

export function render(...) {
  if (createRoot) {
    ...
  }

  ...
}

因为在 React 17 之前,react-dom/client 不存在,而在 React 19 render 也不存在于 react-dom 中。所以直接 import 的兼容代码在两个版本中都会编译报错。

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@lanzhi-lee
Comment options

Answer selected by lanzhi-lee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants