Files
ant-design/.dumi/theme/utils/renderReactToHTML.tsx
lijianan 6d703575ff site: use renderReactToHTMLString (#55219)
* site: use renderReactToHTMLString

* update

* Apply suggestion from @gemini-code-assist[bot]

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: lijianan <574980606@qq.com>

* Apply suggestion from @gemini-code-assist[bot]

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: lijianan <574980606@qq.com>

* update

* type: update type

* update lint

---------

Signed-off-by: lijianan <574980606@qq.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: thinkasany <480968828@qq.com>
Co-authored-by: 遇见同学 <1875694521@qq.com>
2025-10-09 17:10:56 +08:00

19 lines
751 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type React from 'react';
import { flushSync } from 'react-dom';
import { createRoot } from 'react-dom/client';
// 这个方法是将 React 组件转换为 HTML 字符串,作用和 renderToString API 一样
// 根据 React 官方文档的解释,不建议在客户端使用 renderToString API所以这里使用 createRoot + innerHTML 的方式来实现
// https://zh-hans.react.dev/reference/react-dom/server/renderToString
export const renderReactToHTMLString = (node: React.ReactNode) => {
const div = document.createElement('div');
const root = createRoot(div);
// eslint-disable-next-line react-dom/no-flush-sync
flushSync(() => {
root.render(node);
});
const html = div.innerHTML;
root.unmount();
return html;
};