简体   繁体   English

反应儿童的正确 typescript 类型是什么?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM