mirror of
https://github.com/ant-design/ant-design.git
synced 2026-02-09 02:49:18 +08:00
* 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>
19 lines
751 B
TypeScript
19 lines
751 B
TypeScript
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;
|
||
};
|