ali*_*ien 11 javascript typescript reactjs
我收到这个奇怪的 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>
}Run Code Online (Sandbox Code Playgroud)
它们的使用方式如下:
<Container>foo</Children>
Run Code Online (Sandbox Code Playgroud)
然后我得到这些打字稿错误:
'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'.
Run Code Online (Sandbox Code Playgroud)
如果我删除if (!isSpecial) return children并将其更改为if (!isSpecial) return <span>{children}</span>,它就可以正常工作。为什么它不允许我返回字符串?如何在 TypeScript 中解决这个问题?
Fez*_*sta 15
TypeScript 的 React 类型不正确,并且不允许string作为组件类型的返回值。
React 组件允许返回以下类型(React Node):
您可以轻松地测试此运行下面的代码片段并观察组件是否正确呈现。
function HelloWorld() { return "Hello, World!"; }
ReactDOM.render(React.createElement(HelloWorld), document.getElementById("root"));Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
您还可以注意到,运行ReactDOM.render("Hello, World!", ...)也能正常工作,因为renderReact DOM 的函数需要一个有效的 React Node,而且string确实如此。
但您也可以使用包isNode的函数prop-types或Flow 的React$Node类型定义作为参考,来验证 React Node 类型接受什么,这两种类型定义都是由Facebook Meta 本身创建的。
TypeScript 维护者似乎对解决此问题以及错误ReactNode接受的不正确类型不感兴趣undefined,因此您只能诉诸解决方法,例如将文本包装到 Fragment 中,如其他评论中所述。
在您的情况下,children 是一个字符串,因此您应该用 React.Fragment 或 <></> 包装它,但如果 Children 是如下所示的元素,则不会抛出错误
<Container><p>foo</p></Container>
Run Code Online (Sandbox Code Playgroud)
所以只需添加反应片段
import React from "react";
const Container = (props) => {
const isNew = true; // make an api call...
if (isNew) {
return <NewContainer {...props} />;
} else {
return <OldContainer {...props} />;
}
};
const NewContainer = ({ children }) => {
const isSpecial = true;
if (!children) {
return null;
}
if (!isSpecial) {
return <React.Fragment>{children}</React.Fragment>;
// or return <>{children}</>;
}
return <a>{children}</a>;
};
const OldContainer = ({ children }) => {
const isSpecial = useIsSpecial();
if (!children) {
return null;
}
if (!isSpecial) {
return <React.Fragment>{children}</React.Fragment>;
// or return <>{children}</>;
}
return <a>{children}</a>;
};
const Main = () => (
<Container>
<a>foo</a>
</Container>
);
export default Main;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16837 次 |
| 最近记录: |