React with TypeScript - define defaultProps in stateless function

前端 未结 7 1550
梦如初夏
梦如初夏 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:16

    After 2 hours of looking for solution... it's working.

    If you want to define defaultProps, your arrow function should look like:

    export const CenterBox: React.SFC = props => {
        (...)
    };
    

    Then you can define props like:

    CenterBox.defaultProps = { someProp: true }
    

    Note that React.SFC is alias for React.StatelessComponent.

    I hope that this question (and answer) help somebody. Make sure that you have installed newest React typings.

提交回复
热议问题