reactjs – 如何仅允许特定元素类型作为使用Flow和React的子元素
发布时间:2020-12-15 05:07:11 所属栏目:百科 来源:网络整理
导读:我正在尝试重现Flow文档中有关指定子prop的元素类型的步骤.这个例子是官方的 docs. 我目前正在反应原生环境中测试它,使用: React Native(0.55.4) 反应(16.3.1) 流量(0.72.0) 我正在测试的代码是: // @flowimport * as React from 'react'import { StyleShe
我正在尝试重现Flow文档中有关指定子prop的元素类型的步骤.这个例子是官方的
docs.
我目前正在反应原生环境中测试它,使用: > React Native(0.55.4) 我正在测试的代码是: // @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> )
对我来说,它似乎使用“容器”作为类组件
Try Flow website example (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |