简体   繁体   English

打字稿 - 反应0.14无状态功能组件

[英]Typescript - React 0.14 stateless functional components

Simple example: 简单的例子:

import * as React from 'react'
declare function getFish(x: any): any;
declare var Tank: any;

var Aquarium = ({species}) => (
  <Tank>
      {getFish(species)}
  </Tank>
);
let x = <Aquarium species="rainbowfish"/>;

Result: 结果:

(10,9): error TS2605: JSX element type 'Element' is not a constructor function for JSX elements. (10,9):错误TS2605:JSX元素类型'Element'不是JSX元素的构造函数。

Note the error is in regards to the usage of the Component (let x declaration). 请注意,错误与Component的使用有关(让x声明)。 It seems as if maybe the definition file for React is not allowing this as a valid JSX? 似乎React的定义文件可能不允许将其作为有效的JSX? I am using the latest React 0.14 definition file from tsd, what am I doing wrong? 我使用tsd的最新React 0.14定义文件,我做错了什么?


Say I have defined the following stateless functional component (React v0.14) 假设我已经定义了以下无状态功能组件(React v0.14)

let GreeterComponent = (props: {name: string}){
    return <div>Hi {props.name}!</div> 
}

in a different component, I am using it like this: 在另一个组件中,我使用它像这样:

class WrappingComponent extends React.Component{
    render(){
        let names = ['tom', 'john', 'simon'];
        return (
           <div className="names">
               {names.map((name)=> <GreeterComponent name={name} />)}
           </div>
          );
    } 
}

I am getting this error from typescript compiler: 我从typescript编译器得到这个错误:

error TS2605: JSX element type 'Element' is not a constructor function for JSX elements. 错误TS2605:JSX元素类型'Element'不是JSX元素的构造函数。 Property 'render' is missing in type 'Element'. “元素”类型中缺少属性“render”。

How can I fix it? 我该如何解决? What is the correct to work with stateless functional components in typescript? 在打字稿中使用无状态功能组件的正确性是什么? I am using the latest react.d.ts from tsd 我正在使用tsd的最新react.d.ts

When you define a stateless component, it's a plain and simple function. 定义无状态组件时,它是一个简单而简单的函数。

When you instantiate it (ie pass it to React.createElement ), it gets wrapped with React.StatelessComponent . 当您实例化它( React.createElement其传递给React.createElement )时,它将被React.StatelessComponent包装。

error TS2605: JSX element type 'Element' is not a constructor function for JSX elements. 错误TS2605:JSX元素类型'Element'不是JSX元素的构造函数。 Property 'render' is missing in type 'Element'. “元素”类型中缺少属性“render”。

That will work fine in TypeScript latest npm install typescript@latest . 这将在TypeScript最新的npm install typescript@latest

I had to install the typescript using @next : 我不得不使用@next安装打字稿:

npm install typescript@next --save-dev


At the moment of this answer, the @next version is 1.9.0-dev.20160217 . 在这个答案的那一刻, @next版本是1.9.0-dev.20160217

In the beginning I though the problem was with the react.d.ts or react-dom.d.ts ... but it was not. 在开始我虽然问题是react.d.tsreact-dom.d.ts ......但事实并非如此。 The latest version of these are already installed here. 这里已经安装了最新版本。 But the real problem was I installed typescript version 1.7.5, the latest release. 但真正的问题是我安装了最新版本的typescript version 1.7.5。


Some notes about this: as per the blog post Using typescript@next nightly package? 关于这个的一些注意事项:根据博客文章使用typescript @ next nightly package? Don't rely on –save-dev to be up-to-date! 不要依赖-save-dev来保持最新状态! , you cannot rely on npm updating your dependency of typescript to a greater version number after installing with @next and --save-dev . ,你可以不依赖于npm更新您的依赖typescript与后安装在更大版本号@next--save-dev

As they explain, npm update seems not to update from one beta version to the next: 正如他们解释的那样, npm update似乎不会从一个beta版本更新到下一个版本:

eg The dependency string "^1.6.0-dev.20150818" (inside the package.json ) will have the package updated to 1.6.0-dev.20150825 , but not to 1.7.0-dev.20150901 when running npm update 例如,依赖字符串"^1.6.0-dev.20150818" (在package.json )将更新为1.6.0-dev.20150825 ,但在运行npm update时不会npm update1.7.0-dev.20150901

What they recommend is to change the dependency string that npm --save-dev saved inside your package.json to just "next" to be always with the latest of the latest version: 他们建议将npm --save-dev保存在package.json的依赖关系字符串更改为"next" ,以便始终使用最新的最新版本:

{
  ...
  "devDependencies": {
    ...
    "typescript": "next",
    ...
  }
}

I personally didn't try it, because I like to have full control over the version I am using. 我个人没有尝试过,因为我喜欢完全控制我正在使用的版本。 I rarely run npm update anyway. 无论如何,我很少运行npm update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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