這裏是一個使用?
符號這是什麼類型的語法,它是什麼意思? 「變量?:值」
export type Props = {
/**
* The CSS class name of the scroll button elements.
*/
buttonClassName?: string,
}
tabs: ?HTMLElement = undefined;
這裏是一個使用?
符號這是什麼類型的語法,它是什麼意思? 「變量?:值」
export type Props = {
/**
* The CSS class name of the scroll button elements.
*/
buttonClassName?: string,
}
tabs: ?HTMLElement = undefined;
這是用於使用流的javascript/react組件的類型轉換。
讓得到你的例子
export type Props = {
buttonClassName?: string,
}
這些道具出口(主要爲Button類)。這意味着對於某個按鈕組件,buttonClassName的一個屬性是一個可選參數(由於?),其數據類型是一個字符串。如果沒有?在開發環境中會引發警告。
另一個例子如何使用此方法
type ButtonProps = {
label: string,
onClick: Function,
styleClass?: {[key: string]: string}
};
class ButtonComponent extends Component<ButtonProps> {
... other code
}
現在,如果你在你的代碼中使用<ButtonComponent />
你會做
<ButtonComponent
label="Confirm"
onClick={someClickHandlerFunction}
styleClass={a style object}
/>
現在注意到的styleClass的道具。語法意味着它將是一個對象,而問號意味着它是可選的。這種方式可以避免由於未定義的對象等原因而發生大量錯誤。
<ButtonComponent
label="Confirm"
styleClass={a style object}
/>
這將拋出一個警告,作爲支撐的onClick處理不當未獲通過。這樣可以消除很多可能的錯誤。
我建議在開發時使用Atom插件來處理流水線上的代碼。
謝謝!我一定會加入這個插件 –
https://stackoverflow.com/questions/37632760/what-is-the-question-mark-for-in-a-typescript-parameter-name – epascarello
@epascarello謝謝,但這是在一個.js文件。這就是我的困惑所在。 –
' - >'https://flow.org/ –