我收到了这个奇怪的 TypeScript 错误:
import React from 'react'
type Props = {
children: string
}
const Container = (props: Props) => {
const isNew = true // make an api call...
if (isNew) {
return <NewContainer {...props} />
} else {
return <OldContainer {...props} />
}
}
const NewContainer = ({ children }: Props) => {
const isSpecial = useIsSpecial()
if (!children) {
return null
}
if (!isSpecial) {
return children
}
return <a>{children}</a>
}
const OldContainer = ({ children }: Props) => {
const isSpecial = useIsSpecial()
if (!children) {
return null
}
if (!isSpecial) {
return children
}
return <a>{children}</a>
}
那些被这样使用:
<Container>foo</Children>
然后我得到这些 typescript 错误:'NewContainer' cannot be used as a JSX component.
Its return type 'string | Element' is not a valid JSX element.
Type 'string' is not assignable to type 'Element'.
'OldContainer' cannot be used as a JSX component.
Its return type 'string | Element' is not a valid JSX element.
Type 'string' is not assignable to type 'Element'.
如果我删除 if (!isSpecial) return children
, 并将其更改为 if (!isSpecial) return <span>{children}</span>
,它工作正常。为什么它不允许我返回一个字符串?如何在 TypeScript 中解决这个问题?
最佳答案
TypeScript 的 React 类型不正确并且不允许 string
作为组件类型的返回值。
一个 React 组件可以返回以下类型(一个 React 节点):
您可以轻松地运行下面的代码片段来测试它并观察组件是否正确呈现。
function HelloWorld() { return "Hello, World!"; }
ReactDOM.render(React.createElement(HelloWorld), document.getElementById("root"));
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
<div id="root"></div>
您还可以注意到运行
ReactDOM.render("Hello, World!", ...)
也可以,因为 render
React DOM 的函数需要一个有效的 React 节点,并且 string
就是这样。但您也可以使用
isNode
function of the prop-types
package 作为引用或 Flow's React$Node
type definition ,为了验证 React Node 类型接受什么,这两种类型定义都是由 Facebook Meta 本身创建的。TypeScript 维护人员似乎对修复此问题不感兴趣,以及不正确的
ReactNode
错误接受的类型 undefined
,因此您只能求助于解决方法,例如将文本包装到片段中,如其他评论中所述。
关于javascript - 如何在 TypeScript React 组件中返回字符串或 JSX 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65708107/