2017-03-02 56 views
1

我试图将流实现到我的React应用程序中。到目前为止它工作正常,但我遇到了默认值的问题。React无状态组件上的流类型别名

函数头是:

const FormControls = ({ form, controls = null, labels = {} }: { form: any, controls: ?TFormControls, labels?: TFormControlLabels}) => (

为TFormControls类型别名是:

type TFormControls = { 
    onSubmit?: boolean, 
    onReset?: boolean, 
    onClear?: boolean 
} 

我所期望的,因为我把也许运营商那里controls: ?TFormControls,它要么是我的type-alias或null/undefined,但流程告诉我:

src/components/forms/FormControls.jsx:35 
35: const FormControls = ({ form, controls = null, labels = {} }: { form: any, controls: ?TFormControls, labels?: TFormControlLabels}) => (
            ^^^^^^^^ null. This type is incompatible with 
35: const FormControls = ({ form, controls = null, labels = {} }: { form: any, controls: ?TFormControls, labels?: TFormControlLabels}) => (
            ^^^^^^^^ object type 

src/components/forms/FormControls.jsx:35 
35: const FormControls = ({ form, controls = null, labels = {} }: { form: any, controls: ?TFormControls, labels?: TFormControlLabels}) => (
            ^^^^^^^^ object type. This type is incompatible with 
35: const FormControls = ({ form, controls = null, labels = {} }: { form: any, controls: ?TFormControls, labels?: TFormControlLabels}) => (
            ^^^^^^^^ null 

任何指针wo非常欢迎!

编辑:按照要求,错误的完整功能的example

+0

你可以发布一个完整的例子吗?这会让那些试图帮助你的人更容易。你甚至可以在http://flowtype.org/try中发布一个链接 –

回答

1

你在找什么是defaultProps class property

特别,这里是你如何定义你的道具参数:

type FormControlsPropsType = { 
    form: Object, 
    controls: TFormControls, 
    labels: TFormControlLabels 
}; 

const FormControls = ({ 
    form, 
    controls, 
    labels 
} : FormControlsPropsType): React.Element<*> => (
    // ...etc 
); 

这里是你如何定义defaultProps:

FormControls.defaultProps = { 
    controls: null, 
    labels: {}, 
} 

最后,因为你定义默认不存在理由用可选参数定义PropsType。你会看到我删除了?的。

通过在参数定义中设置默认值,您可能已经与React$Element内部件发生冲突。

这里是一个working FlowType example