TypeScriptでReactを使用しており、ステートレス関数を作成しました。私はこの例から無用なコードを削除して読みやすくしています。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に記載されているとおり:
(...) 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. (...)
それはのように簡単にする必要があります:
CenterBox.defaultProps = {
minHeight: 250
}
しかし、このコードはTSLintエラーを生成します。error TS2339: Property 'defaultProps' does not exist on type '(props: CenterBoxProps) => Element'.
だから、再び:どのように正確に私はでき上記のスタック(React + TypeScript)にdefaultProps
を定義しますか?
defaultPropsの仕組みに関する具体的な例を追加できますか?ありがとう –