Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only allow a specific element type as children using Flow and React?

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:

  • React Native (0.55.4)
  • React (16.3.1)
  • Flow (0.72.0)

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>
)
like image 539
Andrio Renan Gonzatti Frizon Avatar asked May 14 '18 19:05

Andrio Renan Gonzatti Frizon


People also ask

How do you make a component React to a child?

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.

How do you use children prop in React?

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.

Can you pass Props to children React?

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.

What is a child element in React?

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.


1 Answers

For me, it seems to work using "Container" as a class component

Try Flow website example

like image 194
Ramon Diogo Avatar answered Nov 12 '22 11:11

Ramon Diogo