Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flow type annotation for children react elements

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>
  );
};
like image 725
jhegedus Avatar asked Nov 17 '16 09:11

jhegedus


2 Answers

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/

like image 96
locropulenton Avatar answered Sep 23 '22 07:09

locropulenton


It doesn't look like.

From the official React libdef:

declare module react {
  declare var Children: any;
  ...
}

and then

declare function cloneElement<Config> (
    element: React$Element<Config>,
    attributes: $Shape<Config>,
    children?: any
  ): React$Element<Config>;
like image 22
Gabriele Petronella Avatar answered Sep 20 '22 07:09

Gabriele Petronella