我尝试使用来自renderToPipeableStream的React18函数。它起作用了,但我不能很好地处理这根管子。
我想这是我代码的重要部分。html是html的字符串数组。我将字符串拆分到中间,在其中我想要放置react代码。
server.get("/", (req: any, res: any) => {
res.write(html[0] + '<div id="root">')
const stream = ReactDomServer.renderToPipeableStream(
<App />
).pipe(res, { end: false })
stream.on("end", () => {
res.write('</dev>' + html[1])
res.end()
})
})问题是stream.on("end"没有开火。但是我希望这样做,因为没有html代码是不完整的。浏览器忽略了这一点,但这并不好。
发布于 2022-03-31 08:09:56
这对我起了作用:
const stream = renderToPipeableStream(jsx, {
onAllReady() {
res.end(`</div></body></html>`);
},
});
stream.pipe(res, { end: false });
res.flush();https://reactjs.org/docs/react-dom-server.html#rendertopipeablestream
https://stackoverflow.com/questions/71458580
复制相似问题