我正在使用React與TypeScript並創建了無狀態函數。爲了便於閱讀,我從示例中刪除了無用的代碼。與TypeScript反應 - 在無狀態函數中定義defaultProps
interface CenterBoxProps extends React.Props<CenterBoxProps> {
minHeight?: number;
}
export const CenterBox = (props: CenterBoxProps) => {
const minHeight = props.minHeight || 250;
const style = {
minHeight: minHeight
};
return <div style={style}>Example div</div>;
};
一切都很好,這段代碼工作正常。但是我的問題是:如何爲CenterBox
組件定義defaultProps
?正確哪能:
正如在react docs提到:如此反覆error TS2339: Property 'defaultProps' does not exist on type '(props: CenterBoxProps) => Element'.
:
CenterBox.defaultProps = {
minHeight: 250
}
但這個代碼生成TSLint錯誤:
(...) They are pure functional transforms of their input, with zero boilerplate. However, you may still specify .propTypes and .defaultProps by setting them as properties on the function, just as you would set them on an ES6 class. (...)
,因爲它應該很容易在我的上面的堆棧(React + TypeScript)中定義defaultProps
?
你可以在如何獲得defaultProps工作上添加一個更具體的例子嗎?感謝:D –