React with TypeScript - define defaultProps in stateless function

前端 未结 7 1544
梦如初夏
梦如初夏 2021-02-01 04:56

I\'m using React with TypeScript and I\'ve created stateless function. I\'ve removed useless code from the example for readability.

interface CenterBoxProps exte         


        
7条回答
  •  再見小時候
    2021-02-01 05:27

    And here's how it works for stateful functions in case others stumble on this. The key is declaring defaultProps as a static variable.

    interface IBoxProps extends React.Props {
        x?: number;
        y?: number;
        height?: number;
        width?: number;
    }
    
    interface IBoxState {
        visible?: boolean;
    }
    
    export default class DrawBox extends React.Component {
        static defaultProps: IBoxProps;
    
        constructor(props: IBoxProps) {
            super(props);
        }
        ...
    }
    
    DrawBox.defaultProps = {
        x=0;
        y=0;
        height=10;
        weight=10;
    };
    

提交回复
热议问题