2016-09-13 4 views
4

나는 나의 순수한 기능적 구성 요소에 대한 defaultprops을 정의 할 수 있지만 형식 오류 얻을 :typescript에서 stateless react 구성 요소에 대한 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) => (
    // ... 
); 

이 그럼 당신은 ( defaultProps의 유형 PageProps 될 것입니다) any으로 캐스팅 할 필요없이 Page.defaultProps를 작성할 수 있습니다.

관련 문제