flow type annotation for children react elements

jhegedus picture jhegedus · Nov 17, 2016 · Viewed 9.1k times · Source

Is there a better way to type-annotate children ?

type FilterLinkProps={
  filter:State$VisibilityFilter,
  children:any
};

const FilterLink = ({
  filter,
  children
}:FilterLinkProps) => {
  return (
    <a href='#'
      onClick={e => {
        e.preventDefault();
        store.dispatch(({
          type: 'SET_VISIBILITY_FILTER',
          filter
        }:Action$VisibilityFilter));
      }}
    >
    {children}
    </a>
  );
};

Answer

locropulenton picture locropulenton · Mar 19, 2017

Flow v0.53 supports children out of the box!!

import * as React from 'react';

type Props = {
  children?: React.Node,
};

More info read official docs or the following blog post about it.

For older versions of flow you can do the following:

import React from 'react';
import type { Children } from 'react';
type Props = {
  children?: Children,
};
const SampleText = (props: Props) => (
  <div>{props.children}</div>
);

Either case children should be declared as a nullable type.

It looks like that they will move some pieces forward with the arrival of fiber, hope they do!

Following the discussion in github

Cheat sheet: http://www.saltycrane.com/blog/2016/06/flow-type-cheat-sheet/