React 元素的流类型

Flow type for React element

这真的是关于 React 或 React Native 中使用类型的无文档。

我发现 render() function 中的元素类型是 React$Element<any>。所以我在我的代码中尝试这样做:

// @flow
import React from 'react';
import { Text } from 'react-native';

function greetingTextNode(string: string): React$Element<any> {
  return (
    <Text>Greeting, { string }.</Text>
  )
}

但它在控制台中显示流类型错误:

 20:     <Text>Greeting, { string }.</Text>
         ^^^^^^ Text. This type is incompatible with
 97: type _ReactClass<DefaultProps, Props, Config: $Diff<Props, DefaultProps>, C: React$Component<DefaultProps, Props, any>> = Class<C>;
                                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ React$Component. See lib: /private/tmp/flow/flowlib_243350bf/react.js:97

如何更正反应元素的流类型?

尝试:

function greetingTextNode(str: string): React.Element<*> {
  return (
    <Text>Greeting, { str }.</Text>
  )
}

您的概念正确,只是语法不正确