[英]What is the correct typescript type for react children?
I'm trying to properly type the props for a component that maps children:我正在尝试正确键入映射子组件的道具:
type Props = {
children: any
}
const MyComponent: FunctionComponent<Props> = () => (React.Children.map(children, someMapingFunction);
I've been using JSX.Element
but that doesn't quite feel right.我一直在使用
JSX.Element
,但感觉不太对劲。
Looking through the code under DefinitelyTyped it appears that children
is typed as ReactNode
.翻阅
ReactNode
下的代码, children
的类型似乎是ReactNode 。
Example:例子:
type Props = {
children: ReactNode
}
const MyComponent: FunctionComponent<Props> = () => (React.Children.map(children, someMapingFunction);
Note : The ReactNode
type can be found in the React namespace:注意:
ReactNode
类型可以在 React 命名空间中找到:
import React from 'react';
let someNode: React.ReactNode;
Actually you don't have to specify children
if you're using React.FunctionComponent
.实际上,如果您使用
React.FunctionComponent
,则不必指定children
项。
For example the following codes compile without error:例如下面的代码编译没有错误:
const MyComponent: React.FC<{}> = props => {
return props.children
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.