2017-03-02 54 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