在 Typescript 中使用 ReactDomServer.renderToString 的正确方法是什么
What's the proper way of using ReactDomServer.renderToString with Typescript
我正在开发一个执行服务器端渲染的 Typescript 应用程序,但我无法理解 ReactDomServer.renderToString
和 ReactDom.hydrate
的用法,特别是它们应该接收的参数类型。
我发现了几个示例(使用 JS)通过以下方式将 JSX 标记作为参数传递:
ReactDomServer.renderToString(<ExampleComponent exampleProp="anyValue" />);
但是当我在 Typescript 上尝试此操作时,出现以下错误:
Conversion of type 'RegExp' to type 'ExampleComponent' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
我让它工作的方法是在我的组件上调用 React.createElement
:
ReactDomServer.renderToString(
React.createElement(ExampleComponent, {exampleProp: anyValue})
)
是否必须在要渲染的组件上调用 React.createElement
(如果是,为什么)?还是我这边的bug/misconfiguration?
我的问题是文件扩展名是 .ts
,打字稿需要 .tsx
才能正确编译 JSX。
我正在开发一个执行服务器端渲染的 Typescript 应用程序,但我无法理解 ReactDomServer.renderToString
和 ReactDom.hydrate
的用法,特别是它们应该接收的参数类型。
我发现了几个示例(使用 JS)通过以下方式将 JSX 标记作为参数传递:
ReactDomServer.renderToString(<ExampleComponent exampleProp="anyValue" />);
但是当我在 Typescript 上尝试此操作时,出现以下错误:
Conversion of type 'RegExp' to type 'ExampleComponent' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
我让它工作的方法是在我的组件上调用 React.createElement
:
ReactDomServer.renderToString(
React.createElement(ExampleComponent, {exampleProp: anyValue})
)
是否必须在要渲染的组件上调用 React.createElement
(如果是,为什么)?还是我这边的bug/misconfiguration?
我的问题是文件扩展名是 .ts
,打字稿需要 .tsx
才能正确编译 JSX。