我正在使用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 –