首页 文章

如何只允许特定元素类型作为使用Flow和React的子元素?

提问于
浏览
6

我正在尝试重现Flow文档中有关指定子prop的元素类型的步骤 . 这个例子是官方的docs .

我目前正在反应原生环境中测试它,使用:

  • React Native(0.55.4)

  • 反应(16.3.1)

  • 流量(0.72.0)

我正在测试的代码是:

// @flow

import * as React from 'react'
import { StyleSheet, View, Text } from 'react-native'

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
})

const Item = () => <Text>Item</Text>

type Props = {
  children: React.ChildrenArray<React.Element<typeof Item>>,
}

const Container = (props: Props) => <View>{props.children}</View>

export default () => (
  <View style={styles.container}>
    <Container>
      <Item />
      <View />
    </Container>
  </View>
)

预期的行为:从流中获取错误,因为我在 Container 组件中使用了 View 组件,但我没有收到任何错误 .

我还尝试了在try flow website上仅使用React的等效版本,但我也没有收到来自流程的错误:

/* @flow */

import * as React from 'react'

const Item = () => <span>Item</span>

type Props = {
  children: React.ChildrenArray<React.Element<typeof Item>>,
}

const Container = (props: Props) => <div>{props.children}</div>

export default () => (
  <div>
    <Container>
      <Item />
      <Item />
      <span>Text</span>
    </Container>
  </div>
)

1 回答

相关问题