[英]What's the proper way of using ReactDomServer.renderToString with Typescript
I'm working on a Typescript app that does server-side rendering and I'm having trouble understanding the usage of ReactDomServer.renderToString
and ReactDom.hydrate
, specifically the type of arguments they sould receive.我正在开发一个执行服务器端渲染的 Typescript 应用程序,但我无法理解
ReactDomServer.renderToString
和ReactDom.hydrate
的用法,特别是它们应该接收的参数类型。
I've found several examples (using JS) passing a JSX tag as the argument the following way:我发现了几个示例(使用 JS)通过以下方式传递 JSX 标记作为参数:
ReactDomServer.renderToString(<ExampleComponent exampleProp="anyValue" />);
But when I try this on Typescript I get the following error:但是当我在 Typescript 上尝试这个时,我收到以下错误:
Conversion of type 'RegExp' to type 'ExampleComponent' may be a mistake because neither type sufficiently overlaps with the other.
将 'RegExp' 类型转换为 'ExampleComponent' 类型可能是错误的,因为这两种类型与另一种类型都没有足够的重叠。 If this was intentional, convert the expression to 'unknown' first.
如果这是故意的,请先将表达式转换为“未知”。
The way I got it working is by invoking React.createElement
on my Component:我让它工作的方式是在我的组件上调用
React.createElement
:
ReactDomServer.renderToString(
React.createElement(ExampleComponent, {exampleProp: anyValue})
)
Do you have to call React.createElement
on the component you want to render (if so, why)?您是否必须在要渲染的组件上调用
React.createElement
(如果是,为什么)? Or is it a bug/misconfiguration on my side?还是我这边的错误/配置错误?
我的问题是文件扩展名是.ts
,打字稿需要是.tsx
才能正确编译 JSX。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.