I'm trying to reproduce the steps on Flow docs regarding specifying element type of the children prop. The example is on the official docs.
I'm currently testing this on a react native environment, using:
The code I'm testing is:
// @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>
)
Expected behavior: get an error from flow because I'm using a View
component inside the Container
component, but I'm not receiving any error back.
I've also tried an equivalent version using only React on the try flow website, but I also receive no error back from flow:
/* @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>
)
TLDR: You can create a reusable Layout component in React without knowing its nested components by making use of React's “children” prop. By passing in the children prop to your Layout component, you can add JSX of { children } to your component which will inject any nested components into Layout when you call it.
By invoking them between the opening and closing tags of a JSX element, you can use React children for entering data into a component. The React children prop is an important concept for creating reusable components because it allows components to be constructed together.
React components use props to communicate with each other. Every parent component can pass some information to its child components by giving them props. Props might remind you of HTML attributes, but you can pass any JavaScript value through them, including objects, arrays, and functions.
children is a special property of React components which contains any child elements defined within the component, e.g. the divs inside Example above. {this. props. children} includes those children in the rendered result.
For me, it seems to work using "Container" as a class component
Try Flow website example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With