2016-09-13 16 views
4

私は私の純粋な機能性成分のためdefaultpropsを定義したいが、私は型エラーを取得:typescriptでステートレス反応コンポーネントのdefaultPropsを定義する方法は?

export interface PageProps extends React.HTMLProps<HTMLDivElement> { 
    toolbarItem?: JSX.Element; 
    title?: string; 
} 

const Page = (props: PageProps) => (
    <div className="row"> 
     <Paper className="col-xs-12 col-sm-offset-1 col-sm-10" zDepth={1}> 
      <AppBar 
       title={props.title} 
       zDepth={0} 
       style={{ backgroundColor: "white" }} 
       showMenuIconButton={false} 
       iconElementRight={props.toolbarItem} 
      /> 
      {props.children} 
     </Paper> 
    </div> 
); 

Page.defaultProps = { 
    toolbarItem: null, 
}; 

私はこれを書くことができることを知っている:

(Page as any).defaultProps = { 
    toolbarItem: null, 
}; 

defaultPropsを追加するための良い方法はありますか?

答えて

7

あなたはこのようPageを入力することができます。

const Page: StatelessComponent<PageProps> = (props) => (
    // ... 
); 

次にあなたがanyにキャストすることなく、Page.defaultPropsを書くことができます(defaultPropsのタイプはPagePropsになります)。

関連する問題