返回在打字稿中反应16个数组元素 [英] Return react 16 array elements in typescript

查看:45
本文介绍了返回在打字稿中反应16个数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新的react 16功能在我的 render 中返回数组元素但是我得到的是typescript错误属性'type'在'Element []'类型中缺少

I want to use the new react 16 feature to return array elements in my render but i'm getting the typescript error Property 'type' is missing in type 'Element[]'

const Elements: StatelessComponent<{}> = () => ([
  <div key="a"></div>,
  <div key="b"></div>
]);

我缺少什么?使用 @ types / react 16.0.10 typescript 2.5.3

推荐答案

<我检查了最新的打字,他们忘了在无状态组件界面中添加新定义。我提出了这个问题,应尽快解决。

I checked the latest typings and they forgot to add new definitions in a stateless component interface. I've raised the issue and it should be fixed soon.

从类组件返回一个数组是有效的,所以如果你现在真的需要它,你可以将你的功能组件转换为类组件。

Returning an array from class components works so if you really need it right now you can transform your functional component to class component.

class Elements extends React.Component<{}> {

  render() {
    return [
       <div key="a"></div>,
       <div key="b"></div>
    ]

  }
}

或暂时使用模块扩充来扩展React类型。只需将以下代码放在某个.ts文件中,typescript就会自动检测定义中的更改。

or temporarily extend React typings using module augmentation. Just put the following code somewhere in one of your .ts files and typescript will automatically detect changes in definitions.

declare module "react" {
  interface StatelessComponent<P = {}> {
    (props: P & { children?: ReactNode }, context?: any): ReactElement<any>[] | ReactElement<any> | null;
    propTypes?: ValidationMap<P>;
    contextTypes?: ValidationMap<any>;
    defaultProps?: Partial<P>;
    displayName?: string;
  }
}

这篇关于返回在打字稿中反应16个数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆