现在后台是流式输出 markdown 字符串,并且每次我也不断处理全量的 markdown 字符串,再使用MarkdownContent
这个组件去渲染成 html
但这样会导致一个问题,就是在流式输出的过程中,由于会不断地执行这个MarkdownView
组件,导致无法使用鼠标选中渲染后的文本,等下次 render 时,选中状态就又丢失了
有什么方案能够增量更新吗,不操作以前的 dom
// 大概就这样的逻辑
function App() {
const [message, setMessage] = useState('')
async function foo() {
const reader = response.body?.getReader();
const decoder = new TextDecoder();
let result = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
result += decoder.decode(value);
setMessage(result);
}
}
return <MarkdownContent source={message}></MarkdownContent>;
}
export const MarkdownContent = memo(({ source }: MarkdownContentProps) => {
const html = useMemo(() => markdown.render(source ?? ""), [source])
return (
<article
className='prose dark:prose-invert'
style={{ maxWidth: "100%" }}
dangerouslySetInnerHTML={{ __html: html }}
/>
)
})
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.