2016-11-17 30 views
10

有沒有更好的方式來鍵入註釋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> 
); 
}; 

回答

8

它看起來不像。

從官方React libdef

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

然後

declare function cloneElement<Config> (
    element: React$Element<Config>, 
    attributes: $Shape<Config>, 
    children?: any 
): React$Element<Config>; 
23

流量v0.53支持children開箱!

import * as React from 'react'; 

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

更多信息閱讀official docs或它下面的blog post

對於舊版本的流程,你可以做以下的

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

兩種情況children應被宣佈爲nullable類型。

看起來他們會隨着纖維的到來向前移動一些碎片,希望他們能做到!

github

速查表討論:http://www.saltycrane.com/blog/2016/06/flow-type-cheat-sheet/

0
type Text = string | number; 
type Fragment = Iterable<Node>; 
type ReactNode = React$Element<any> 
    | Text 
    | Fragment; 

ReactNode應該是孩子的類型,但買者自負。

來源:我相信在流程回購的一些github問題上我見過這種構造。